Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Understanding IF statement in SP

Author  Topic 

cipriani1984
Constraint Violating Yak Guru

304 Posts

Posted - 2011-01-12 : 06:47:29
Hi,

I just wanted to ask why the following IF's wont work in SQL, but would work in Java, ASP etc.

I have the following IF statement in my SP.

select @datatype = [Data_Type] from dbo.table
WHERE ID = 2

IF @datatype = 'APP_CAT'
BEGIN

END

ELSE IF @datatype = 'CAT_CAT'
BEGIN

END

ELSE IF @datatype = 'CAT_TOPIC'
BEGIN

END

Any help / explaniation would be much appreciated! :)

Sachin.Nand

2937 Posts

Posted - 2011-01-12 : 06:59:22
What do you mean by wont work in SQL ?

declare @datatype varchar(10)
select @datatype = 'APP_CAT'

IF @datatype = 'APP_CAT'
BEGIN
print '1'
END

ELSE IF @datatype = 'CAT_CAT'
BEGIN
print '2'
END

ELSE IF @datatype = 'CAT_TOPIC'
BEGIN
print '3'
END



PBUH

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-12 : 08:49:00
i doubt the variable @datatype that OPs is using doesnt have enough length to hold full value causing it to be truncated or it is of type char.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-01-12 : 10:16:25
Post the full code used in the procedure

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -