November 23, 2010 at 9:44 pm
I need to put a IF EXISTS condition in a stored procedure.
But Query for IF EXISTS is stored in a variable
like
LINE 1 @IfExistsQuery='some query'
LINE 2 IF EXISTS(@IfExistsQuery)
LINE 3 BEGIN
Do SOME PROCESSING...
LINE 5 END
Here On Line 2 its giving error :
An Expression of non-boolean type specified in a context where a condition is expected.
Please suggest what to do as my query is coming from variable.
November 23, 2010 at 11:23 pm
You cannot execute Dynamic queries in this manner
You will have to do some changes as shown below
DECLARE @strSQL NVARCHAR(100)
DECLARE@iCount INT
SET @strSQL = 'SELECT @iCount = COUNT(*) FROM dbo.TableName'
EXECUTE sp_executeSQL @strSQL, N'@iCount INT OUTPUT', @iCount OUTPUT
IF @iCount > 0
BEGIN
-- Do some processing
END
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply