Question : using string inside a sql string with EXECUTE sp_executesql

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 ?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
-- 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

Answer : using string inside a sql string with EXECUTE sp_executesql

try this
1:
2:
3:
4:
5:
6:
7:
8:
9:
-- 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
Random Solutions  
 
programming4us programming4us