?
Solved

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

Posted on 2007-11-29
5
Medium Priority
?
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1800 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 200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this article, we’ll look at how to deploy ProxySQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

752 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