views:

35

answers:

1

Hi all, I am trying to execute the following query. I don't have 'CrewID' column so in that case it will by pass update part of the script. but it gives error Invalid object CrewID'. Can you please tell me why it excute update part even my if condition does not matched. Is there is another way to do the same. I have the requirement where need to rename the column but before rename i have to copied data in other column and need to excute script many times.

if exists (select * from syscolumns where name ='CrewID' and id in (select id from dbo.sysobjects where id = object_id(N'[dbo].[WorkPlanAssignees]') and OBJECTPROPERTY(id, N'IsUserTable') = 1))
BEGIN   
update A set A.TempCrewID=B.ID from WorkPlanAssignees A inner join Crew B on A.CrewID=B.ID
END
A: 

SQL is not executed line by line. It's compiled as a batch. At compile time, the column does not exists. So it gives an error. The IF is irrelevant.

Use dynamic SQL or let us know what you're trying to do.

And asked before:

Stored Procedure consist Add column, Update data for that column, and Select all data from that table

gbn
Thanks... yes its Compiletime error. I think dynamic sql will solve my problem.
Harendra