sp_executesql and IF EXISTS

  • I have a procedure that uses the syscolumns and sysobjects table to check a table field by field (check the entries in records two by two (a stored result record and a calculated value record) to see if there are differences between them). This works fine, but generates loads of empty grids for all the fields for which no differences are found.

    I would therefore like to use an IF EXISTS clause, to check the result of the statement before actually executing it.

    However, a construction like

    IF EXISTS (sp_executesql @sql) generates an INVALID SYNTAX error.

    Any suggestions?

  • One might guess that you need to put everything including IF EXISTS into a string and execute that. However, you are aware of http://www.sommarskog.se/dynamic_sql.html ? And may I add that using the INFORMATION_SCHEMA views might also be better, since they are ANSI defined and won't change, while the structure of the system tables can change at a service pack level.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Thanks. I already did something like including everything in the string. Works fine now.

    Thank you for the link to that article, as well. Can be of use. I guess I'd better experiment a bit with the INFORMATION_SCHEMA views

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply