Search code examples
sqlsql-servert-sqlvalidationsqlcmd

Disable SQLCMD Syntax Check


We sometimes write dataport scripts that rename tables or columns and have other complex logic. We also have SQL in IF statements referencing those old columns or tables. We have a standard to use IF statements to make our SQL Scripts multi run friendly.

However occasionally, even though the if statement evaluates to false, the code wrapped in the block errors out, I'm guessing because of a syntax check.

if 1 = 0
begin

   select * from mydatabase.dbo.mytable; -- doesn't execute, but still errors because doesn't exist

end

Another weird thing is it isn't always consistent, sometimes it works fine.

Does anybody know if a way I can disable these sort of checks on a script by script basis.

Thanks

NOTE

I know people are going to say they tried a similar situation and it doesn't error. At first for me it didn't error in SSMS but it errored in SQLCMD.

sqlcmd MyTestScript.sql -r 1 -U user -P password

Then I put SSMS in SQLCMD mode Query -> CMDMODE and it still didn't give the error. Then after rerunning a few times it started to error in both.


Solution

  • I think execute_sql built in stored procedure can work around this.