Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to pass multivalue based on single value Expand / Collapse
Author
Message
Posted Tuesday, February 25, 2014 7:34 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, June 2, 2014 7:13 AM
Points: 45, Visits: 151
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
Post #1545194
Posted Tuesday, February 25, 2014 8:12 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 11:16 PM
Points: 839, Visits: 5,422
join to a table-valued function using CROSS APPLY?
Post #1545201
Posted Wednesday, February 26, 2014 4:31 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 6:56 AM
Points: 162, Visits: 355
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
Post #1545326
Posted Wednesday, February 26, 2014 7:42 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 11:50 AM
Points: 13,082, Visits: 12,547
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1545397
Posted Wednesday, February 26, 2014 10:39 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 9:45 AM
Points: 115, Visits: 638
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.



Post #1545493
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse