Solved

dynamic sql in SQL Server 2005 Can't put quotes around value

Posted on 2007-11-29
5
415 Views
Last Modified: 2012-08-13
Hi,
I am trying to convert a stored proc to dynamic SQL.  The reason is because a SSRS report uses multi-select dropdowns.  Checking “Select All” does not answer all the users needs.  At times they also have to have any null values that might be included in that particular parameter.

To get around this I added an “All Possible Values” choice.  To make this work I’ve had to convert the proc to dynamic SQL.  There is one problem that I haven’t been able to solve.

In order to process the multivalue string a function is called.  However in this case the code is being passed in without surrounding single quotes and SQl Server thinks that it’s a column name.  If I surround the parameter with quotes I get a different error.

Here is a sample calling of the proc:
exec p_Report_IndividualPortfolio1Test  'TX,CA'  

Here is the input function:

      @StateCodeMultiValue    varchar(max)

Here is the code that has the error:
IF @StateCodeMultiValue not like '000'
      BEGIN
            SET @StateCode = 'AND Portfolio ' +
                  'IN (SELECT splitvalues ' +
                  'FROM dbo.f_SplitMultiValueString  ' +
                    @StateCodeMultiValue +  ',' +  ''','''  + '))'
                              
SET @SQL_WHERE = @SQL_WHERE + @StateCode
      END
ELSE SET @SQL_WHERE = @SQL_WHERE

Below is the original code that works when dynamic SQL was not needed.
AND MAT1_03_06 IN (
            SELECT splitvalues FROM dbo.f_SplitMultiValueString  (@StateCodeMultiValue,','))

The error returned is below:
WHERE 1 = 1 AND Portfolio IN (SELECT splitvalues FROM dbo.f_SplitMultiValueString  TX,CA,',')

The error returned is:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'TX'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'CA'.
Msg 8144, Level 16, State 3, Line 1
Procedure or function dbo.f_SplitMultiValueString has too many arguments specified.

So it seems ot me that the problem is that I figure out how to surround the passed string with single quotes.  I can put them around the parameter (@StateCodeMultiValue) but that won’t work either.

Thanks for any help and I’ll do my best to clarify.

FS

0
Comment
Question by:FairSkies
  • 3
5 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20379080
put double quotes around it
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20379110
           SET @StateCode = 'AND Portfolio ' +
                  'IN (SELECT splitvalues ' +
                  'FROM dbo.f_SplitMultiValueString  ''' +
                    @StateCodeMultiValue +  ''','',''))'
0
 
LVL 25

Accepted Solution

by:
imitchie earned 450 total points
ID: 20379118
missing the (

SET @StateCode = '
AND Portfolio IN (
   SELECT splitvalues
   FROM dbo.f_SplitMultiValueString (''' + @StateCodeMultiValue +  ''','',''))'

(sql server accept multi line strings)
0
 
LVL 6

Assisted Solution

by:PaultheBroker
PaultheBroker earned 50 total points
ID: 20379655
mitch is right, you need the final output to be interpreted as "FROM dbo.f_SplitMultiValueString('TX')"
but it is being interpreted as "FROM dbo.f_SplitMultiValueString(TX)".  If you want to print a single quote in the middle of a string like this, you have to escape it with another single quote - so '' (two single quotes) will be printed as ' (one single quote).  Cutting and pasting mitch's example above shows that he actually typed two single quotes, not a double quote - so it looks like we all agree :)
SET @StateCode = '

AND Portfolio IN (

   SELECT splitvalues

   FROM dbo.f_SplitMultiValueString (''' + @StateCodeMultiValue +  ''','',''))'

Open in new window

0
 

Author Comment

by:FairSkies
ID: 20383042
Thanks for the quick responses.  This was exactly what I needed.  It will help greatly on the rest of this project.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now