filter conditions based on condition..

  • well its a bit confusing to say like i want to enable a condition based on a specific condition..

    explaining this , my front end has an employee ID dropdown list with options --All--,rest of the emp Ids

    when --All-- is selected, all the employees should be displayed and i want this in a single query

    if exists (select top 1 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME='test_data')

    drop table test_data

    create table test_data(Ename nvarchar(max),Eid int, Esalary int)

    insert into test_data(Ename,Eid,Esalary) values('me',1,100)

    insert into test_data(Ename,Eid,Esalary) values('you',2,300)

    insert into test_data(Ename,Eid,Esalary) values('x',3,350)

    insert into test_data(Ename,Eid,Esalary) values('ab',4,450)

    insert into test_data(Ename,Eid,Esalary) values('abc',41,4500)

    --this doesn't work

    select * from test_data where Eid in case when @param='%' then Eid in (select Eid from test_data) else Eid end

    -- managing currently like this

    select * from test_data where Eid like @param -- gives all the values if @param='%' if @param=3 only specific value.

    drop table test_data

    is there any other way around to do this..?

  • I think you're looking at catch all queries.

    Try this blog post by Gail[/url]


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Your selection logic is flawed. You should read Gail's article, but I think that this would be closer to what you need:

    select * from test_data

    where Eid = (case when @param='%' then Eid else @param end)

    No doubt you already know this, but using select * is generally considered bad practice.


  • thanks Phil, i do know the implications of *..

  • Cadavre (1/5/2012)


    I think you're looking at catch all queries.

    Try this blog post by Gail[/url]

    -- thanks for the way.. works better now..:)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply