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
 General SQL Server Forums
 New to SQL Server Programming
 simple if statement, what am i missing

Author  Topic 

ericsimard
Starting Member

1 Post

Posted - 2007-10-13 : 11:44:20
Hi everyone and thank for taking the time to read, I am new to this forum and to SQL to.
I am taking a programmers course, i learned mainly C# for now, but i also know a bit of C and C++ and linux programming,
i am going through my SQL school book, (SQL Server 2000 design and implementation) and i am trying a simple IF statement. heres how it goes
DECLARE @P CHAR
SET @P=NULL

SELECT @P=Powerlck FROM vehicle
IF (@P=NULL)
PRINT 'got you'


powerlck is a collum taking char(1) which is either Null or 1.
there is five entry all but one is set to 1, null is the default

when i run this script, it does not print 'got you'

but if i run select * from vehicle
it will show one with <NULL> value.

what did i miss??

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-10-13 : 11:48:12
(1) IF (@P IS NULL ). No two NULLs are same so you cannot compare @p = NULL.
(2) Also define the size of your parameters. CHAR(10) instead of somply CHAR else it willd efault to 1 and your values will get truncated.



Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2007-10-14 : 05:12:42
You could try someting like :
DECLARE @P CHAR(10)
SET @P=NULL

SELECT @P=ISNULL(Powerlck,'-1') FROM vehicle
IF (@P = '-1')
PRINT 'got you'



Jack Vamvas
--------------------
Search IT jobs from multiple sources- http://www.ITjobfeed.com/SQL
Go to Top of Page
   

- Advertisement -