Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable TSQL script check?

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 :-)

like image 772
grady Avatar asked Mar 29 '10 15:03

grady


People also ask

How do I disable a check constraint in SQL Server?

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.

How do I stop a SQL script from running?

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.

How do I disable full text search?

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.


2 Answers

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...

like image 187
gbn Avatar answered Sep 30 '22 18:09

gbn


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.

like image 20
Thomas Avatar answered Sep 30 '22 20:09

Thomas