SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

pkuchaliya
Starting Member

India
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

@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)

Singapore
17587 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
      )
. . . .



KH
Time is always against us

Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6059 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)
etc...

Be One with the Optimizer
TG
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.05 seconds. Powered By: Snitz Forums 2000