I have a dynamic SQL statememt in a stored procedure that checks to see if a certain column name exists. I want to create an IF statement that executes the query and if the name exists (if the executed statement comes back true), execute some other code. I can't seem to get the IF THEN syntax right. Any ideas?
Example:
BEGIN
DECLARE @q VARCHAR(8);
SET @q = column name;
IF SELECT @sql = 'SELECT cname FROM sys.syscolumns
WHERE cname = @q';
EXECUTE (@sql); IS TRUE THEN
execute some code;
END IF;
END;
Example:
BEGIN
DECLARE @q VARCHAR(8);
SET @q = column name;
IF SELECT @sql = 'SELECT cname FROM sys.syscolumns
WHERE cname = @q';
EXECUTE (@sql); IS TRUE THEN
execute some code;
END IF;
END;