tags:

views:

85

answers:

4

I am trying to add 2 columns and then I would like to set some values to them but I get a compile-error saying the column does not exist. I am using the following script:

IF @LogProcessed = 0
Begin

IF NOT EXISTS (select column_name from INFORMATION_SCHEMA.columns where table_name = 'SYSTM_FRM' and column_name = 'SF_Ip_TXT')
ALTER TABLE SYSTM_FRM add SF_Ip_TXT NVARCHAR(20)

IF NOT EXISTS (select column_name from INFORMATION_SCHEMA.columns where table_name = 'SYSTM_FRM' and column_name = 'SF_Port_NUM')
ALTER TABLE SYSTM_FRM add SF_Port_NUM int


IF  (EXISTS (select column_name from INFORMATION_SCHEMA.columns where table_name = 'FRM' and column_name = 'FRM_Ip_TXT') AND
    EXISTS (select column_name from INFORMATION_SCHEMA.columns where table_name = 'FRM' and column_name = 'FRM_Ip_TXT'))
begin
Update dbo.SYSTM_FRM
SET     dbo.SYSTM_FRM.SF_Ip_TXT = dbo.FRM.FRM_Ip_TXT,
        dbo.SYSTM_FRM.SF_Port_NUM = dbo.FRM.FRM_Port_NUM

FROM dbo.FRM INNER JOIN dbo.SYSTM_FRM ON dbo.FRM.FRM_RCRD_NUM = dbo.SYSTM_FRM.SF_FrameRecord_NUM          

ALTER TABLE FRM DROP COLUMN FRM_Ip_TXT
ALTER TABLE FRM DROP COLUMN FRM_Port_NUM  
  end


Update [Update_Log]
Set Update_Log_Processed = 1
Where [Update_Log_Version] = '00001'

end

Is there any way to use a column that I am adding in the same script?

+4  A: 

Put a GO in between

Glennular
There is a problem. The whole code is in an IF statement, so when I add GO in between, it gives syntax errors. The whole code is in between BEGIN and END.
sbenderli
+3  A: 

you cannot save it in a proc like that

example

create table TestAdd2 (id int)
go

You cannot create this procedure

create proc prTest as

insert TestAdd2 values (1)

exec ('ALTER TABLE TestAdd2 add SF_Port_NUM int')

update TestAdd2 set id = 1,SF_Port_NUM = 2

select * from TestAdd2

GO

You get this error Msg 207, Level 16, State 1, Procedure prTest, Line 7 Invalid column name 'SF_Port_NUM'.

This is because at parse time the column does not exist

However if you use dynamic SQL for the update you are good to go

create proc prTest2 as

insert TestAdd2 values (1)

exec ('ALTER TABLE TestAdd2 add SF_Port_NUM int')

exec ('update TestAdd2 set id = 1,SF_Port_NUM = 2')

select * from TestAdd2

GO

in your case your update statement would be

exec('Update dbo.SYSTM_FRM
SET     dbo.SYSTM_FRM.SF_Ip_TXT = dbo.FRM.FRM_Ip_TXT,
        dbo.SYSTM_FRM.SF_Port_NUM = dbo.FRM.FRM_Port_NUM
FROM dbo.FRM INNER JOIN dbo.SYSTM_FRM 
ON dbo.FRM.FRM_RCRD_NUM = dbo.SYSTM_FRM.SF_FrameRecord_NUM')
SQLMenace
How do I use dynamic SQL?
sbenderli
see code that I added
SQLMenace
you need dynamic sql in your update statement as well because of parse time compilation problems
SQLMenace
What'ss the purpose of this line: 'update TestAdd2 set id = 1,SF_Port_NUM = 2'
sbenderli
It is an example that you can run so that you can see how it works, in your case it would be exec('Update dbo.SYSTM_FRMSET dbo.SYSTM_FRM.SF_Ip_TXT = dbo.FRM.FRM_Ip_TXT, dbo.SYSTM_FRM.SF_Port_NUM = dbo.FRM.FRM_Port_NUMFROM dbo.FRM INNER JOIN dbo.SYSTM_FRM ON dbo.FRM.FRM_RCRD_NUM = dbo.SYSTM_FRM.SF_FrameRecord_NUM')
SQLMenace
It works, thank you!
sbenderli
A: 

It should work, could be that the column which does not exist is from FRM table?

František Žiačik
A: 

All DDL statements should follow the statement seperator GO in order to make use of it

Madhivanan