Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello :)

I tried to use IF Statement within my stored procedure in SQL SERVER 2008 R2

but there are errors that I don't know how to fix it.

any help please ?

here is my stored procedure

SQL
USE [MsaGroup_SYTS]
GO
/****** Object:  StoredProcedure [dbo].[InserNewcommentFromViewPages]    Script Date: 03/05/2012 02:08:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[InserNewcommentFromViewPages]
@_UserID Int,
@_Text NVarChar(MAX),
@_Type NVarChar(50),
@_Date DateTime,
@_ItemID Int
as
insert
into
Comments
(
Comments.[User_ID],
Comment_Text,
Comment_Type,
Comment_Date,
Comment_TypeID,
Comment_FlagIsActive,
Comment_Original
)
values
(
@_UserID,
@_Text,
@_Type,
@_Date,
@_ItemID,
'true',
'~/pages/' + @_Type + 'view.aspx?ID=' + @_ItemID + '&Image='
+ (IF (@_Type  = 'Parteners') 
BEGIN
   SELECT Partners.Partner_ImageGalleryID from Partners where Partner_ID = @_ItemID
END
ELSE
BEGIN
	IF (@_Type  = 'News') 
	BEGIN
		SELECT News.News_ImageGalleryID from News where News.News_ID = @_ItemID
	END
END
ELSE
BEGIN
	IF (@_Type  = 'Prodects') 
	BEGIN
		SELECT Products.Product_ImageGalleryID from Products where Product_ID = @_ItemID
	END
END
ELSE
BEGIN
	IF (@_Type  = 'Ads') 
	BEGIN
		SELECT Ads.Ads_ID from Ads where Ads.Ads_ID = @_ItemID
	END
END
)
+ '&Video='
+ (IF (@_Type  = 'Parteners') 
BEGIN
   SELECT Partners.Partner_VideoGalleryID from Partners where Partner_ID = @_ItemID
END
ELSE
BEGIN
	IF (@_Type  = 'News') 
	BEGIN
		SELECT News.News_VideoGalleryID from News where News.News_ID = @_ItemID
	END
END
ELSE
BEGIN
	IF (@_Type  = 'Prodects') 
	BEGIN
		SELECT Products.Product_VideoGalleryID from Products where Product_ID = @_ItemID
	END
END
ELSE
BEGIN
	IF (@_Type  = 'Ads') 
	BEGIN
		SELECT Ads.Ads_ID from Ads where Ads.Ads_ID = @_ItemID
	END
END
)
)
Posted
Comments
graciax8 4-Mar-12 20:22pm    
what is the error?
Varun Sareen 4-Mar-12 22:59pm    
what error?

just a hint..

may be you have to cast the int to string

i mean this
SQL
'~/pages/' + @_Type + 'view.aspx?ID=' + @_ItemID + '&Image='


into this
SQL
'~/pages/' + @_Type + 'view.aspx?ID=' + cast(@_ItemID as nvarchar(max)) + '&Image='


and i would cast the IDs that you select too

NOTE : are you sure that the select return a single value?
 
Share this answer
 
v2
It looks like you're attempting to use IF in the middle of the SELECT statement? IF is for procedural Transact-SQL. For IF functionality within a query, you'd need CASE - http://www.techrepublic.com/article/in-t-sql-use-casewhen-in-place-of-ifthen/5078041[^]
Scott
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900