Link to home
Start Free TrialLog in
Avatar of homnath_sharma
homnath_sharma

asked on

Execute Immediate syntax..

Hi,

Hope to get the solution for the below from expert asap..

SET @query = @query + ' FROM DBO.' + @sTable + ' WHERE ' + @sTable +'.ComponentId = '    + CONVERT(CHAR, @id);

EXECUTE IMMEDIATE @query;

Whats wrong with the above syntax, which i am trying to implement inside my cursor. I am getting error as:

Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'IMMEDIATE'. The stored procedure will still be created.

Awaiting to hear your support on this asap.

Thanks in advance.
~ HNS ~
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Remove the IMMEDIATE and run

EXEC @Variable.

Best Regards,

Paulo Condeça.
EXECUTE IMMEDIATE is a statement used in C language. In MS_SQL you hane EXECUTE or EXEC and you can also use the sp_executesql stored procedure, so either

EXEC(@query)   (you have to use brackets)

or

EXEC sp_executesql @query  (barackets are not required)

using sp_executesql gives you the advantage that you can pas into the @query parameters and also you cna use OUT parameters to get data from the execution.