|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, April 05, 2013 8:10 PM
Points: 958,
Visits: 2,873
|
|
|
|
|
|
SSC 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
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, April 18, 2013 2:49 AM
Points: 65,
Visits: 65
|
|
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)+',%'
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 7:11 AM
Points: 877,
Visits: 1,159
|
|
Nice article contains good examples, description.
Thanks
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, April 18, 2013 10:20 AM
Points: 32,
Visits: 44
|
|
| But if you are having to use a table, would you not just do a join now instead of doing an IN ?
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 1:13 PM
Points: 3,578,
Visits: 5,119
|
|
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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:51 PM
Points: 42,
Visits: 351
|
|
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!
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 12:13 PM
Points: 130,
Visits: 512
|
|
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.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 4:27 PM
Points: 3,226,
Visits: 64,229
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Sunday, December 02, 2012 10:52 PM
Points: 5,
Visits: 97
|
|
Good article. I still think though using User-Defined Table Types is easier and faster
|
|
|
|