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
 dynamic query

Author  Topic 

pkuchaliya
Starting Member

49 Posts

Posted - 2008-09-05 : 08:14:39
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

@age,@salary,@designation,@department

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.

pankaj

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-05 : 08:22:33
you don't need dynamic SQL

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



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-05 : 08:23:50
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)
etc...

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -