Alter table and update with ADO



Hello,

I am using an ADO SQLQuery to check to see if structure updates are needed
for a database and apply them if they are needed. In one case I am checking
to see if a field exists in a table, and if not I add the field and then I
need to update the new field to contain data from another table (this isn't
going to be a foreign key by the way). But the query errors out saying the
field doesn't exist. Here's the statement:

if not exists (SELECT *
FROM SYSCOLUMNS
WHERE ID = OBJECT_ID('TABLENAME')
AND Name = 'NEWCOL'
)
begin
alter table [TABLENAME]
add NEWCOL integer;

Update tablename set newcol=(select X from Othertable where
y=tablename.y);
end;

The query fails with 'Field newcol not found'. If I run this in Query
Analyzer I could use the 'Go' statements to make sure each statement is
executed when it needs to be so this doesn't happen, but I don't know of an
equivelent using ADO. Is there an equivalent or is there another way to
write this sql statement so this will work? Thanks!


.



Relevant Pages

  • Re: Alter table and update with ADO
    ... > alter table ... > add NEWCOL integer; ... >The query fails with 'Field newcol not found'. ...
    (borland.public.delphi.database.ado)
  • Re: Alter table and update with ADO
    ... > I am using an ADO SQLQuery to check to see if structure updates are ... > needed for a database and apply them if they are needed. ... > But the query errors out saying the field doesn't exist. ... > add NEWCOL integer; ...
    (borland.public.delphi.database.ado)
  • Re: Simple select using calculated column
    ... You cannot refer to a column alias in the query (the one exception is in ... select dbo.functionas newcol ... "Earl Teigrob" wrote in message ... > I have a query like ...
    (microsoft.public.sqlserver.programming)
  • Simple select using calculated column
    ... I have a query like ... from tablename ... everything works fine without the where clause ... I get an error that newcol is an invalid column name ...
    (microsoft.public.sqlserver.programming)