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
 PLS HELP

Author  Topic 

Peter7218
Starting Member

2 Posts

Posted - 2013-04-22 : 09:33:25
I am puzzled. What is the purpose of this query?

declare @empName varchar(200),
@salary money
set @empName=null
set @salary=1000
select *
from Employee_Test
where (@empName is null or Emp_name=@empName)
and (@salary is null or Emp_Sal=@salary)

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-22 : 09:36:31
Purpose is:

If you enter any value, then retrieves records which satisfies that condition
Otherwise it will show all records (in the case of @empname = null or @salary = null)

--
Chandu
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-22 : 09:41:44
Kinda quiz?? :)

Employess without name but salary as 1000

Cheers
MIK
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-22 : 11:55:30
quote:
Originally posted by MIK_2008

Kinda quiz?? :)

Employess without name but salary as 1000

Cheers
MIK



Nope

its just retrieving list of employees who have the passed salary value (1000)

the empname = null is to bypass the employeename filter

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Peter7218
Starting Member

2 Posts

Posted - 2013-04-22 : 19:21:56
Any expert out there, PLEASE help to solve my puzzle.
Go to Top of Page

logpop
Starting Member

16 Posts

Posted - 2013-04-22 : 19:44:57
It lists all employees with salary 1000.
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-04-22 : 19:50:57
As visakh16 explained, the query returns all records where employee salary is 1000.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-04-22 : 21:16:19
Just a word of caution: when you use this technique to bypass search conditions in a stored procedure, that can result in some extremely bad execution plans. See Sommarskog's article here for analysis and alternatives: http://www.sommarskog.se/dyn-search-2008.html

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-23 : 12:41:48
quote:
Originally posted by Peter7218

Any expert out there, PLEASE help to solve my puzzle.


whats the puzzle? isnt my statement answer your puzzle?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -