Subject Re: [ib-support] DDL updates
Author Milan Babuskov
yeohray wrote:
> In SQL Server, I could update a table's definition by first making
> some checks on the system tables e.g.
>
> IF NOT EXISTS (SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID
> ('mytable') AND Name = 'newcolumn')
> UPDATE mytable ADD newcolumn INTEGER

You meant: ALTER mytable, right?

>
> Can I do the same somehow in Firebird?

The syntax would be similar:

IF NOT EXISTS (select 1 from RDB$RELATION_FIELDS where RDB$FIELD_NAME =
'newcolumn' and RDB$RELATION_NAME = 'mytable') THEN
ALTER mytable ADD newcolumn INTEGER;

> Also, what is PSQL? I see this mentioned a lot in the release notes.

AFAIK, PSQL is sql used in stored procedures and triggers. So you have a
problem here. You cannot use IF (...) in regular DSQL, only inside
triggers and stored procedures, and you cannot execute DDL statements in
PSQL, so you really can't do this kind of stuff with Firebird.

You can do it programatically: first select to see if there is a column
with:

select 1 from RDB$RELATION_FIELDS where RDB$FIELD_NAME = 'newcolumn' and
RDB$RELATION_NAME = 'mytable';

and then (if needed) run query to create it:
ALTER mytable ADD newcolumn INTEGER;

If I'm wrong, then someone please correct me, since this is one of
"nice" features in MSSQL that don't exist in FB. If Firebird is to allow
this, then it would be possible to write .sql batches that update
database structure if necessary.

--
Milan Babuskov
http://fbexport.sourceforge.net