Question

I have an old existing interbase table and I want to add a primary key field to and populate it. Is there any way to do it all in the SQL statement (like SQL server). Example:

ALTER TABLE IBUSERS ADD IBUSERSPK VARCHAR(32) default (newid()) NOT NULL

As far as I can tell in interbase newid function does not exist unless I am missing something.

I am using IBExpert and also have IBConsole.

Or am I stuck with populating this field in code after it gets created?

Thanks.

Was it helpful?

Solution

It appears interbase has no easy way to SQL populate a guid field. Therefore the solution is to create your own UDF function in the database to create a random guid. It then becomes a three step process:

Add the field to the table

ALTER TABLE IBUSERS ADD IBUSERSPK  VARCHAR(32) NOT NULL

Populate the new field using the UDF:

UPDATE IBUSERS SET IBUSERSPK = GETGUID()

Add primary key constraint if it is such:

ALTER TABLE  IBUSERS ADD CONSTRAINT PK_IBUSERS PRIMARY KEY (IBUSERSPK)

The nice thing about this is that then this function can be used anytime/anywhere in the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top