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.
| Author |
Topic |
|
raviborra
Starting Member
14 Posts |
Posted - 2008-02-13 : 13:06:55
|
| Hi,This is the table which i have.1 100 Ravi IT 200002 200 SitaRam CSE 300003 300 Rams EEE NULL5 400 Kabir IT NULLAnd part of my SP has as follows.DECLARE @salary INT;SET @salary=NULL;DECLARE @DeptName VARCHAR(20);SET @DeptName='IT';IF EXISTS(SELECT * FROM Employee WHERE DeptName = @DeptName AND salary = @salary)BEGINprint 'Record existed in the table'ENDELSE BEGIN print 'Record not existed in the table'ENDActually record is existed with that combination, how this query will work seemlessly in all the situation.Meaning, how to check for NULL existance with out using "IS NULL"Thanks,Ravi. |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-02-13 : 13:13:53
|
| declare @Employee table ( c1 int, c2 int, EmpName varchar(200), DeptName varchar(200), salary numeric(10,2))insert into @Employeeselect 1 , 100 , 'Ravi', 'IT', 20000 union select 2, 200, 'SitaRam', 'CSE', 30000 union select 3, 300, 'Rams', 'EEE', NULL union select 5, 400, 'Kabir', 'IT', NULL select * from @EmployeeDECLARE @salary INT;SET @salary=NULL;DECLARE @DeptName VARCHAR(20);SET @DeptName='IT';IF EXISTS(SELECT * FROM @Employee WHERE DeptName = @DeptName AND IsNull(salary,0) = IsNull(@salary,0))BEGINprint 'Record existed in the table'ENDELSE BEGIN print 'Record not existed in the table'END"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
raviborra
Starting Member
14 Posts |
Posted - 2008-02-13 : 13:20:01
|
| Hi,Thanks a lot; It works.Thanks,Ravi. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 04:21:52
|
| Why dont you use this?IF EXISTS(SELECT * FROM Employee WHERE DeptName = @DeptName AND (@salary is null or salary = @salary)MadhivananFailing to plan is Planning to fail |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-02-14 : 08:33:14
|
| If you set @salary to null and the salary in the DB is not null, then you will still get results((@salary is null AND salary is NULL) or salary = @salary)"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
|
|
|
|
|