October 27, 2009 at 5:54 am
Why is this sql generating an error? The reg table has several columns, but it should never get there? I guess it executes the sql anyway? Is there another way to do this? I tried searching for this but it's a little hard to find this problem. Thanks in advance for your help.
if 1=0
begin
insert into reg values(0)
end
October 27, 2009 at 5:59 am
The SQL is parsed before it evaluates the conditions of the IF statement, so you'll need to use dynamic SQL for this, which won't be evaluated until the statement is executed. E.g.:
if 1=0
begin
declare @sql varchar(8000)
set @sql='insert into reg values(0)'
exec (@sql)
end
October 27, 2009 at 6:03 am
I was afraid I'd have to do that. Thanks for the quick reply.
November 2, 2009 at 9:09 am
why wouldn't you just comment this code out or remove it altogether?
For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply