|
I have created a report in which I have to select multiple comma separated values in parameter. When I put only query like Report is working fine for multiple values. But when I put this logic into stored proc. For single value it is working fine but for multiple value it is not working. Can some one please help me in this . 1) logic for stored proc for accepting multiple comma separated values? 2) Is this possible i can make my stored proc dynamic in a way so that it takes database name dynamically (for every new database I don't have to alter my proc each and every time). Thanks
(comments are locked)
|
|
A while ago I wrote an article about passing multi-value params into a stored proc. It can be found here: http://blog.hoegaerden.be/2009/11/21/reporting-on-data-from-stored-procedures-part-2/ That should help you out with Q1. As for Q2: well, yes, you could use dynamic SQL, but it does have some drawbacks. Erland Sommarskog wrote a lot of info regarding that topic on the following page: http://www.sommarskog.se/dynamic_sql.html Take your time to read it through and you'll see how helpful it is! In your particular case, as you need to deal with a random number of databases, you'll probably have to do something like this: loop over all incoming database names and construct each individual query to retrieve data from each DB dynamically. UNION ALL them together and use sp_executesql to fetch the result. Thanks Valentino. This is very helpful.
Nov 29 '11 at 07:32 AM
dev_ritesh
(comments are locked)
|
|
As @Valentino Vr... mentioned in his answer and blog post, you can pass the coma separated list into the stored proc and then split the string to return a table. He presented a method using a while loop... If you want to use a more optimized method for splitting such delimited strings, take a look On @Jeff Moden post http://www.sqlservercentral.com/Forums/Topic943562-338-1.aspx#bm944589 where you can find the fastest known T-SQL solution for splitting strings using a Tally table. @Jeff Moden has also a great post on SQL Server Central Tally OH! An Improved SQL 8K “CSV Splitter” Function. If you want the fastest solution, you can incorporate a CLR. You can find a CLR solution on my blog post Fastest CSV strings splitting using CLR (T-SQL vs. CLR revisited). Thanks Pavel.
Nov 29 '11 at 12:09 PM
dev_ritesh
@Pavel Pawlowski: thanks for that link to Jeff's tally table solution, quite interesting and indeed a good alternative to the split function with a loop! It takes a little longer to decipher though :)
Nov 30 '11 at 06:47 AM
Valentino Vranken
(comments are locked)
|
|
I am getting this error while doing this process. I created function which will covert string into table and using that function in my stored proc. the error is : Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AI" in the equal to operation. Can some one resolve this issue.
(comments are locked)
|
|
Hello try this: And you can use it like: OR OR OR CREATE TABLE #T (Name varchar(100)) insert into #T VALUES ('value1'),('value2'),('value3') OR Thanks yaqubonnet :)
Nov 29 '11 at 12:08 PM
dev_ritesh
(comments are locked)
|

