Comma Delimited List as Stored Procedure Parameter

Comma Delimited List as Stored Procedure Parameter

May 22 2007

The crew at 4 Guys From Rolla have come through for me again (as they frequently do in matters of ASP, .NET and SQL). In building a new webservice, I wanted to be able to pass in a comma-delimited list of ID's to a SQL stored procedure. Unfortunately, there isn't a SQL datatype for something like "comma delimited list". I could have taken the "easy" way out and built the SQL query dynamically in C# instead of calling a stored procedure, but then I would have been mad at myself for breaking the rule that we agreed on that all the webservices would strictly call stored procedures. I figured I'd have to use SQL to parse a passed in string into ID's, and build the SQL query from there. However, this seemed like something someone else probably would have done in the past. I found a few techniques online but this one seemed the cleanest and best-implemented. You basically pass in a big varchar string of id's into the stored procedure and then call a SQL User Defined Function called "Split" which returns a table of the ID's which you can join to. It's simpler than it sounds...instead of having something like

SELECT *
FROM table
WHERE id in (--lots of convoluted code to parse out the ids--)

you just call

SELECT *
FROM table
WHERE id in ( SELECT convert(int,Value) FROM dbo.Split(@list_string,',') )

The complete code for the function and examples of how to use it are listed in the article. Thanks to "4 Guys" for saving me a couple hours and some headaches with this nifty technique!

Share this post:deliciousdiggredditfurlgoogleyahoo
Related Posts
Getting VBScript to Correctly Interpret Number Formats Across Locales
Web Project vs Local Project
International Currencies in ASP
Flash Remoting (via .NET v1.1) with VS 2005

Comments on this post:

Thanks for linking this up. The Split() technique is easy to implement without adding a whole lot of extra code.

Thanks SO MUCH for this. I never would have come up with this most eloquent solution. And shame on SQL Server for requiring it. Love to 4 Guys From Rolla.

Wow! You and the 4 guys should get some sort of an award for this! This has made life so much more stress free!

I wonder how well this would scale? I would imagine it is better to first create a table variable in the local procedure and then use that table in the actual WHERE clause? Otherwise, wouldn't the function have to be evaluated (and re-split) for each row in the table?

Trying to use this for a char based values as in:

SELECT convert(varchar(100), Value) FROM dbo.Split('''ListItem1'', ''ListItem2''',',')

doesn't seem to work.

Eventhough the above statement by itself works and returns back a temp table with two 'listitems' - when used within another select statement with the IN clause doesn't seem to work.

Any ideas why?

Thank you

Athadu

My mistake...

SELECT convert(varchar(100), Value) FROM dbo.Split('ListItem1, ListItem2',',')

works perfectly - i was trying to be too smart and put in too many quotes.

Thank you for the excellent solution.

Regards
Athaud

Hey it worked!!! even with GUIDs!!! i had a function that takes the comma delimited and convert it to a temp table then i would JOIN the function to my query. this worked fine for numbers and guid IN SQL -- although i modified the function to work with GUIDs( parse also '{' and '}' and ',') it worked fine even with guids in sql but i get a cast error(string to guid) in my application although im getting the correct result set in sql and are passing the right paramater types(string/nvarchar). A next alternative i was thinking of was to create a temp table and populate it with the guids(im getting the guids from a chklistbox) maybe call a looped insert statement to populate it then use the temp table in my query either JOIN it or 'Select IN(Select Guids from @temptable)' but i wont have to go thru all of this!!! thanks to this post :) much appreciated!!!

Thanks to "4 Guys From Rolla". Excellent solution provided.

Exactly was I was looking for, thanks for the link up! I

Thank you very much.
This is of great help, as we have to write lesser code and will save our precious time.
online marketing

Terrific work ! Your site has presented me much of the particulars I wished .
Somerset Locksmiths

The hardest part about applying to graduate school is writing the admissions essay, also known as the personal statement. Learn how to improve your essay.

Thanks for linking this up. The Split() technique is easy to implement without adding a whole lot of extra code.
cafetières

However, the main issues of portability SQL RDBMS products there is still a lack of comprehensive interpretations, or different standard. Among these reasons are great, and the incomplete definition of standards and a single supplier.

I almost gave up on a solution for updating multiple rows from one call to the database. This solved my problem ;-)

There are very interesting points in time there in article but I don’t know if I see all of them heart to middle . There is some validity but I will take hold judgement until I look into it further. Good article , thanks and we want more..

It's really great stuff overall, I just wanted to say thank you. I am looking forward to another great article from your side.

There are very informative stuff which I looking for, I'll bookmark this site for further information, such a great job.

I really like it! I'll always appreciate your brief sharing in this awesome stuffs sincerely, this discussion has put light on this topic.

It's really great stuff and I look forward to reading your post. What you have to say is important and should be read.

Post new comment

  • The content of this field is kept private and will not be shown publicly.
    • Allowed HTML tags: <a> <em> <strong> <img> <code> <pre>
    • Lines and paragraphs break automatically.
    More information about formatting options
Archives
December, 2011 (1)
April, 2011 (1)
November, 2010 (1)
May, 2010 (5)
April, 2010 (3)
January, 2010 (2)
November, 2009 (1)
October, 2009 (1)
January, 2009 (1)
October, 2008 (1)
July, 2008 (1)
May, 2008 (3)
April, 2008 (1)
March, 2008 (1)
February, 2008 (1)
January, 2008 (1)
November, 2007 (2)
October, 2007 (1)
September, 2007 (2)
August, 2007 (3)
Tags
.NET ASP award awards Banner blog Campaign Design Development DryJoys Flash FootJoy Forms Hacks Information Architecture Information Archtecture Interaction Internationalization iPad iPhone Launch Microsite Microsoft MITX mobile myjoys Nomenclature PhizzPop Usability Web Standards
Contributors
Brandon (9)
Denis (4)
Denise (31)
Jon (12)