Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

using string inside a sql string with EXECUTE sp_executesql

Posted on 2009-12-21
5
Medium Priority
?
292 Views
Last Modified: 2012-05-08
Hi

I have some sql that I am runing with EXECUTE sp_executesql . When I try to include a string within the sql I get an error "Invalid colum name 'my Msg1' ". But if I don't include a string but a number instead it works fine.

how do I do the first code snippet below ?
-- this does NOT work
SELECT @query =
'SELECT prev_HoleID, prev_dTo, dFrom, info, 
CASE WHEN ([prev_dTo] < [dFrom]) THEN "my Msg 1"  ELSE "my Msg 2" END -- this is the bit with a problem
FROM #StagCheck
WHERE	(NOT (gapCheck IS NULL)) AND (NOT (gapCheck = 0))
ORDER BY prev_HoleID, prev_dTo'
				
EXECUTE sp_executesql @query

-- this DOES work
SELECT @query =
'SELECT prev_HoleID, prev_dTo, dFrom, info, 
CASE WHEN ([prev_dTo] < [dFrom]) THEN 1 ELSE 0 END 
FROM #StagCheck
WHERE	(NOT (gapCheck IS NULL)) AND (NOT (gapCheck = 0))
ORDER BY prev_HoleID, prev_dTo'
				
EXECUTE sp_executesql @query

Open in new window

0
Comment
Question by:LukeB
5 Comments
 
LVL 7

Expert Comment

by:waltersnowslinarnold
ID: 26102338
As what have you declared, @query? varchar or int?
0
 
LVL 39

Accepted Solution

by:
appari earned 2000 total points
ID: 26102348
try this

-- this does NOT work 
SELECT @query = 
'SELECT prev_HoleID, prev_dTo, dFrom, info,  
CASE WHEN ([prev_dTo] < [dFrom]) THEN ''my Msg 1''  ELSE ''my Msg 2'' END -- this is the bit with a problem 
FROM #StagCheck 
WHERE   (NOT (gapCheck IS NULL)) AND (NOT (gapCheck = 0)) 
ORDER BY prev_HoleID, prev_dTo' 
                                 
EXECUTE sp_executesql @query

Open in new window

0
 
LVL 11

Expert Comment

by:Goodangel Matope
ID: 26102361
Its the kind of quotes you are using. You need to use two single quotes instead of one double quote to enclose your strings. See code snippet below that should work.
SELECT @query =
'SELECT prev_HoleID, prev_dTo, dFrom, info, 
CASE WHEN ([prev_dTo] < [dFrom]) THEN ''my Msg 1''  ELSE ''my Msg 2'' END -- this is the bit with a problem
FROM #StagCheck
WHERE   (NOT (gapCheck IS NULL)) AND (NOT (gapCheck = 0))
ORDER BY prev_HoleID, prev_dTo'

Open in new window

0
 
LVL 1

Author Closing Comment

by:LukeB
ID: 31668856
duh, thanks ... only thing i did not try !!
0
 
LVL 14

Expert Comment

by:shru_0409
ID: 26102398
try this

SELECT @query =
"SELECT prev_HoleID, prev_dTo, dFrom, info, CASE WHEN ([prev_dTo] < [dFrom]) THEN "+char(34)+"my Msg 1"+char(34)+"  ELSE "+char(34)+" my Msg 2"+char(34)+" END -- this is the bit with a problem
FROM #StagCheck
WHERE   (NOT (gapCheck IS NULL)) AND (NOT (gapCheck = 0))
ORDER BY prev_HoleID, prev_dTo"

or

SELECT @query =
"SELECT prev_HoleID, prev_dTo, dFrom, info,  
CASE WHEN ([prev_dTo] < [dFrom]) THEN 'my Msg 1'  ELSE 'my Msg 2' END -- this is the bit with a problem
FROM #StagCheck
WHERE   (NOT (gapCheck IS NULL)) AND (NOT (gapCheck = 0))
ORDER BY prev_HoleID, prev_dTo"
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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

578 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