SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to pass multivalue based on single value


how to pass multivalue based on single value

Author
Message
latitiacasta
latitiacasta
SSC-Enthusiastic
SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)

Group: General Forum Members
Points: 182 Visits: 192
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
pietlinden
pietlinden
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4941 Visits: 13193
join to a table-valued function using CROSS APPLY?
serg-52
serg-52
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1191 Visits: 1826
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26297 Visits: 17553
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.

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)
hunchback
hunchback
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 Visits: 639
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.



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search