Author |
Topic |
iminore
Posting Yak Master
141 Posts |
Posted - 2007-05-04 : 10:58:58
|
Trying to run this:if not exists(select O.name from sysobjects O left join syscolumns C on O.id = C.id where O.name = 'tablea' and C.name = 'apparea')beginalter table tablea add apparea varchar(8) NULLupdate tablea set apparea = 'xxx'endI get error:Server: Msg 207, Level 16, State 1, Line 1Invalid column name 'apparea'.Any ideas? |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-05-04 : 11:01:47
|
you need a go between the alter and update_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
iminore
Posting Yak Master
141 Posts |
Posted - 2007-05-04 : 11:09:30
|
get these errors then:Server: Msg 170, Level 15, State 1, Line 4Line 4: Incorrect syntax near 'NULL'.Server: Msg 156, Level 15, State 1, Line 2Incorrect syntax near the keyword 'end'.Surely a 'GO' should never be between a 'begin' and 'end' |
 |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-05-04 : 11:11:54
|
exactly my point.you can't do this the weay you want it to.tryif not exists(select O.name from sysobjects O left join syscolumns C on O.id = C.id where O.name = 'tablea' and C.name = 'apparea')beginexec('alter table tablea add apparea varchar(8) NULL')update tablea set apparea = 'xxx'end_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
iminore
Posting Yak Master
141 Posts |
Posted - 2007-05-04 : 11:23:17
|
Same original error. |
 |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-05-04 : 11:38:06
|
hm... try running the if with out the update and see if the table contains the column._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
iminore
Posting Yak Master
141 Posts |
Posted - 2007-05-04 : 11:41:30
|
It works without the update. The point is if the column is already there I don't want to update the table.It seems the 'add column' does not complete in some sense before the update runs. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-04 : 11:47:17
|
[code]if not exists(select O.name from sysobjects O left join syscolumns C on O.id = C.id where O.name = 'tablea' and C.name = 'apparea')beginalter table tablea add apparea varchar(8) NULLendgoupdate tablea set apparea = 'xxx' where apparea is null[/code] KH |
 |
|
iminore
Posting Yak Master
141 Posts |
Posted - 2007-05-04 : 12:10:18
|
Think you are missing the point.I DON'T want to update the table if the column is already there. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-04 : 12:27:48
|
if the column is already there, the value will not be NULL as you would have update it when you first added the column KH |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-05-04 : 13:13:02
|
You can't change a structure and access it in the same batch.if not exists(select O.name from sysobjects O left join syscolumns C on O.id = C.id where O.name = 'tablea' and C.name = 'apparea')beginexec('alter table tablea add apparea varchar(8) NULL')exec('update tablea set apparea = ''xxx''')end==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
iminore
Posting Yak Master
141 Posts |
Posted - 2007-05-08 : 03:43:00
|
Khtan - I don't see why the value will not be null if already there.nr - spot on. What you say makes sense and it works. many thanks. |
 |
|
|