Validating an SQL query programmatically, with gotcha

If you want to check the validity of a TSQL statement programmatically, you can make use of the SET NOEXEC statement. As this MSDN reference states, SET NOEXEC ON will compile the query but won’t actually execute it. This is ideal (well, nearly….) if perhaps you have a dynamically generated statement that you want to check is valid before executing it. Example time SETNOEXECONGOSELECTTOP*FROMsys.tablesGOSETNOEXECOFFGOThis results in the following error, as I’ve failed to specify the number of rows to return for the TOP: [Read More]