April 27, 2006 at 2:41 pm
DECLARE @id int
SET @id=2
DECLARE @sql varchar(400)
SELECT @sql='SELECT * FROM emp WHERE id='+ @id+ 'ORDER BY id'
EXEC (@sql)
I am getting the following error when I execute the above code.
Syntax error converting the varchar value 'SELECT * FROM emp WHERE id=' to a column of data type int.
What should be the reason and how to fix it. Any help would be highly appreciated.
Thanks.
April 27, 2006 at 3:14 pm
thanks.
April 27, 2006 at 3:25 pm
You might want to add a space in front of the ORDER BY to make sure it doesn't run into the id.
April 28, 2006 at 8:32 am
And may I ask why you are using dynamic sql for this???
May 2, 2006 at 9:46 am
My questions was aimed at him avoiding the use of dynamic sql.. not encourage him :-).
May 2, 2006 at 11:28 am
remi - your question was not clear enough...you should've said - "in the example posted, it is not clear why dynamic sql is being used - try & avoid dynamic sql as much as you would a cursor...if you explain the requirements someone may be able to come up with a solution that eliminates the use of dynamic sql!"
**ASCII stupid question, get a stupid ANSI !!!**
May 2, 2006 at 12:06 pm
Well it's his job at risk (or not), not mine :-).
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply