Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-29
5
Medium Priority
?
462 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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