Solved

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

Posted on 2007-11-29
5
401 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
Comment Utility
put double quotes around it
0
 
LVL 25

Expert Comment

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

Accepted Solution

by:
imitchie earned 450 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the quick responses.  This was exactly what I needed.  It will help greatly on the rest of this project.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Read about achieving the basic levels of HRIS security in the workplace.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

763 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

6 Experts available now in Live!

Get 1:1 Help Now