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 12345»»»

Using a Variable for an IN Predicate Expand / Collapse
Author
Message
Posted Monday, June 20, 2011 10:09 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: Sunday, September 28, 2014 1:41 PM
Points: 959, Visits: 2,888
Comments posted to this topic are about the item Using a Variable for an IN Predicate
Post #1128721
Posted Monday, June 20, 2011 10:41 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 12, 2012 8:44 PM
Points: 30, Visits: 145
Todd, thank you for article.
Did you try UDF that return a table?

Cheers
Post #1128726
Posted Tuesday, June 21, 2011 1:58 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, October 24, 2014 4:53 AM
Points: 67, Visits: 97
too keep it simple whith less code (but only for short in-lists)

Declare @inStr varchar(100)
Set @inStr = '1,2,4,6'


select * from testtable
where ','+@inStr+',' like '%,'+cast(column as varchar)+',%'


Post #1128812
Posted Tuesday, June 21, 2011 4:06 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, November 20, 2014 5:13 AM
Points: 1,131, Visits: 1,394
Nice article contains good examples, description.

Thanks
Post #1128858
Posted Tuesday, June 21, 2011 4:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 12:41 PM
Points: 34, Visits: 60
But if you are having to use a table, would you not just do a join now instead of doing an IN ?
Post #1128865
Posted Tuesday, June 21, 2011 5:40 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 7:10 AM
Points: 4,436, Visits: 6,338
You should absolutely positively NOT use the CTE for this scenario unless you are guaranteed to have VERY few rows split out AND you do not have any signficant data skew. If you have either or both of those you will get screwed with a bad plan at least some of the time. Either a nested loop index seek plan with a kajillion rows or a scan/hash plan with a few rows. The optimizer can't have any idea how many rows are coming out of the split. Nor can the optimizer have statistics on the VALUES of the row(s) coming out of the split.

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1128907
Posted Tuesday, June 21, 2011 6:06 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 12:31 PM
Points: 42, Visits: 441
Thanks Todd - great article. My company uses a similar function for splitting, but I simply have not had the need/opportunity to use it.

I have an upcoming task in which I have to modify a few stored procedures to accept a list for several of their arguments rather than a single value so this will work great. Very timely article for me!
Post #1128925
Posted Tuesday, June 21, 2011 6:08 AM


SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, November 13, 2014 10:04 AM
Points: 141, Visits: 641
Hey Todd

Re: splitting a string to a table variable "array" - give this a try:

DECLARE @values TABLE (value VARCHAR(10))
DECLARE @xml AS XML,@str AS VARCHAR(100),@delimiter AS VARCHAR(10)
SET @str='A,B,C,D,E'
SET @delimiter =','
SET @xml = CAST(('<X>'+REPLACE(@str,@delimiter ,'</X><X>')+'</X>') AS XML)
INSERT INTO @values (value)
SELECT N.value('.', 'varchar(10)') AS value FROM @xml.nodes('X') as T(N)
SELECT * FROM @values

Cheers,
Ken

PS I can't claim to have come up with this - I found it somewhere on the net.
Post #1128928
Posted Tuesday, June 21, 2011 7:11 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, September 30, 2014 7:42 AM
Points: 3,688, Visits: 72,435
And of course those of us with SQL 2008 can pass tables as stored procedure parametes now :)



--Mark Tassin
MCITP - SQL Server DBA
Proud member of the Anti-RBAR alliance.
For help with Performance click this link
For tips on how to post your problems
Post #1128977
Posted Tuesday, June 21, 2011 7:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 4, 2014 8:57 AM
Points: 5, Visits: 98
Good article.
I still think though using User-Defined Table Types is easier and faster
Post #1128996
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse