lets say I have a script like that:
if(some condition) begin select somecolumn from sometable end
Lets say, that "somecolumn" does not exist and the condition is not true, which means the select is NOT executed. Even though the select would not be executed, the script is not valid, Management Studio complains about the missing column "somecolumn".
Question: Can I somehow disable this sort of check so that the script is executed and since the if is not true, it will never notice that the column is missing?
Thanks :-)
To disable a check constraint for INSERT and UPDATE statements. In Object Explorer, expand the table with the constraint and then expand the Constraints folder. Right-click the constraint and select Modify. In the grid under Table Designer, click Enforce For INSERTs And UPDATEs and select No from the drop-down menu.
In SQL Server Configuration Manager, in the left pane, select SQL Server Services. In the results pane, right-click SQL Server (MSSQLServer) or a named instance, and then select Start, Stop, Pause, Resume, or Restart.
On the FTS tab, To disable full text searching, select the Disable Full Text Searching check box. To disable FTS indexer, select the Disable FTS Indexer check box.
Use dynamic SQL
if(some condition)
begin
exec ('select somecolumn from sometable') --or sp_executesql
end
It actually makes no sense to run this because of what SQL is. It's not executed line by line: the whole batch is parsed etc in one go and the error is generated here, before anything actually runs in the sense you mean. This is by design...
You can create a procedure that references a table that does not exist however that is the only exception to the rule. From the BOL:
Deferred name resolution can only be used when you reference nonexistent table objects. All other objects must exist at the time the stored procedure is created. For example, when you reference an existing table in a stored procedure you cannot list nonexistent columns for that table.
Beyond using dynamic SQL, there is no means to reference non-existent columns in a stored procedure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With