November 20th, 2003, 04:02 PM
Join Date: Nov 2003
Time spent in forums: < 1 sec
Reputation Power: 0
I have a query that looks something like this:
DEFINE @VAR_A VARCHAR(6)
DECLARE trsite_cursor CURSOR FOR
SELECT DISTINCT AppField
ORDER BY 1
FETCH NEXT FROM trsite_cursor INTO @VAR_A
WHILE @@FETCH_STATUS = 0
SET @SELCMD ='SELECT * FROM TABLE_2
WHERE Field1 =' + @VAR_A +
' ORDER BY 1,2,3;'
EXEC xp_sendmail @query = @SELCMD,
and the rest of the query......
The column "AppField" in TABLE_1 has been defined as varchar. Let's assume it contains the value: ABCD. When I run it, the query fails at the SET @SELCMD statement, saying that the column name ABCD is invalid. It assumes that ABCD is a column name & not a value. However, if AppField contains a numeric value, ex: 123, I don't get any errors & the query outputs the desired results.
So, I guess, the question is: how do I make the SET @SELCMD treat the value in AppField as "ABCD" or 'ABCD' and not just ABCD?