Solved

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

Posted on 2007-11-29
5
431 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL replication over high latency link 10 60
TSQL Query Into Specific XML Format w/ Multiple Groupings 6 33
SQL Syntax 24 45
Database ERD 4 29
Creating and Managing Databases with phpMyAdmin in cPanel.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

860 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