Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread IF NOT EXIST old syntax
Tue, Dec 18 2007 9:41 AMPermanent Link

"M. Malagoli"
Hi,
in the new syntax of elevate DB how to add a field or costraint only if not
exist?
like the old syntax CREATE INDEX IF NOT EXISTS ind ON "giri"

thanks

Tue, Dec 18 2007 10:51 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

You have to query the system tables. In my conversion app I'm using a query IsThere and a procedure to add the code to make indices to a script


 procedure AddSimple(iName, tbl, fld: string);
var
 sqlcode: string;
begin
 IsThere.Close;
 IsThere.SQL.Text := 'SELECT * FROM Information.Indexes WHERE TableName = ' + QuotedStr(tbl) + ' AND Name = ' + QuotedStr(iName);
 IsThere.ExecSQL;
 if IsThere.RecordCount = 0 then begin
  IdxMaker.SQL.Add('EXECUTE IMMEDIATE');
  sqlcode := StringReplace('CREATE INDEX "$iName" ON "$table" ($field)', '$iName', iName, []);
  sqlcode := StringReplace(sqlcode, '$table', tbl, []);
  sqlcode := StringReplace(sqlcode, '$field', fld, []);
  IdxMaker.SQL.Add(QuotedStr(sqlcode) + ';');
 end;
end;

It is possible to embed the test into the sql creating the index and I know Tim has posted an example somewhere in the ngs.

I'm hoping that EXISTS will make a reappearance.

Roy Lambert
Image