I am trying to get count of a rows with specific values in a table, and if the count is 0, then add a value in the table. This count is a local variable in stored procedure.
I am building the SQL dynamically and storing SQL statement into a nvarchar
variable.
Then, using EXEC
I am running this SQL as follows hoping to populate count variable.
But it's not working.
DECLARE @qry NVARCHAR(max)
DECLARE @count INT
-- building @qry will result as follows
@qry = SELECT @count = COUNT(*) FROM aTable WHERE (col1 = @col1 AND ...)
@count = EXEC @qry
IF @count = 0
BEGIN
-- carry on with adding
END