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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 dynamic query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

49 Posts

Posted - 09/05/2008 :  08:14:39  Show Profile  Reply with Quote
create table timepass(id varchar(50),age int, salary int,designation varchar(50),department varchar(50))

insert into timepass
values('10001',24, 2500,'Manager','IT')

insert into timepass
values('10002',24, 8900,'accountant','IT')

insert into timepass
values('10003',24, 6500,'peon','admin')

insert into timepass
values('10004',24, 8800,'developer','account')

insert into timepass
values('10005',24, 14500,'sr. developer','hr')

insert into timepass
values('10006',24, 9500,'programmer','IT')

hi i have write a procedure in i recieved four parameter


user can search on given parameter and it is not compulsory that he will give all the parameter to search.
but if use want to search only on the basis of age , salary then it will search only given criteria and remainder parameter will assign to '0', and if he want to search all condition then it search all.


In (Som, Ni, Yak)

17689 Posts

Posted - 09/05/2008 :  08:22:33  Show Profile  Reply with Quote
you don't need dynamic SQL

select *
from   timepass
where  (
           @age is null
       or  age = @age
and    (
           @salary is null
       or  salary = @salary
. . . .

Time is always against us

Go to Top of Page

Flowing Fount of Yak Knowledge

6065 Posts

Posted - 09/05/2008 :  08:23:50  Show Profile  Reply with Quote
make sure to validate that at least one parameter is passed has a value (unless you want to allow that all rows could be returned), then:

WHERE age = isNull(@age, age)
AND salary = isNull(@salary, salary)
AND desigination = isNull(@designation, designanation)

Be One with the Optimizer
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000