how to pass multivalue based on single value

  • Hi,

    I have below table

    CREATE TABLE [dbo].[Temptable](

    [name] [nvarchar](50) NULL,

    [namesrt] [int] NULL,

    [addrs] [nvarchar](50) NULL,

    [addrssrt] [int] NULL

    ) ON [PRIMARY]

    INSERT [dbo].[Temptable] ([name], [namesrt], [addrs], [addrssrt]) VALUES (N'cons', 410, N'cc', 1)

    INSERT [dbo].[Temptable] ([name], [namesrt], [addrs], [addrssrt]) VALUES (N'fun', 430, N'hs', 2)

    INSERT [dbo].[Temptable] ([name], [namesrt], [addrs], [addrssrt]) VALUES (N'cons', 410, N'hs', 3)

    INSERT [dbo].[Temptable] ([name], [namesrt], [addrs], [addrssrt]) VALUES (N'fun', 430, N'cc', 4)

    INSERT [dbo].[Temptable] ([name], [namesrt], [addrs], [addrssrt]) VALUES (N'cons', 100, N'all', 5)

    INSERT [dbo].[Temptable] ([name], [namesrt], [addrs], [addrssrt]) VALUES (N'fun', 100, N'all', 6)

    INSERT [dbo].[Temptable] ([name], [namesrt], [addrs], [addrssrt]) VALUES (N'ALL', 599, N'ALL', 7)

    My query is

    Declare @NAME varchar(100)

    SELECT * FROM TEMPTABLE

    WHERE Namesrt in (@NAME)

    I can provide @NAME either 410 or 430

    so when its 410 i need to pass value 410,100,599

    when its 430 then 430,100,599 else 410,430,100,599

    Please provide query for same.

    Thanks in advance

  • join to a table-valued function using CROSS APPLY?

  • You may hard code it as well

    select * from [Temptable]

    where namesrt in (100,599

    , case @NAME when '410' then 410 when '430' then 430 end

    , case when not (@NAME in ('410' , '430') ) then 410 end

    , case when not (@NAME in ('410' , '430') ) then 430 end

    )

    regards

    Serg

  • As you are discovering you can't put a delimited list inside a variable and use IN to find all the values. You have a couple of choices here.

    1) You could create a user defined table type and pass that. This is probably overkill for what you are doing here though.

    2) You could use a string splitter. I recommend the one you would find by following the link in my signature about splitting strings.

    Your final query would end up something like this.

    SELECT *

    FROM TEMPTABLE tt

    CROSS APPLY dbo.DelimitedSplit8K(@Name, ',')

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • We have table-valued parameters since 2008 version.

    http://technet.microsoft.com/en-us/library/bb510489(v=sql.105).aspx

    I would suggest to start from here and just reach to other approaches if you are not able to leverage it.

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

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