Page 1 of 1

How in MyDac to test is a table exists?

Posted: Thu 14 Feb 2013 06:53
by piedsoftware
Hi

I am new to MyDac, and I'm converting a project from Zeos. It is mostly straightforward so far, but one apparent lack in MyDac is an equivalent for the Zeos TZTable property Exists. This allows you to test if the TableName property corresponds with an existing table on the database without opening the component.

So, how can you simply test is a table exists with MyDac components?

TIA
Mark Patterson

Re: How in MyDac to test is a table exists?

Posted: Thu 14 Feb 2013 23:22
by skydvrz
Just do a query (for the default schema):

show tables like 'agoodtablename'

and count the records. Zero records=invalid tablename.

You can enumerate all the tables in the default schema with:

show tables

Add the normal schema name syntax if you want a specific schema.

Re: How in MyDac to test is a table exists?

Posted: Mon 18 Feb 2013 12:59
by DemetrionQ
Hello.

You can use the following ways:

1) use the TMyConnection.GetTableNames method. For example:

Code: Select all

  var SL:TStringList;
...
  SL:=TStringList.Create;
  MyConnection1.Database:='Your_DataBase';
  MyConnection1.GetTableNames(SL);
  if SL.IndexOf('Your_Table')=-1 then
    ShowMessage('Your_Table not exists')
  else
    ShowMessage('Your_Table exists');
  SL.Free;
2) use the TMyMetaData component. For example:

Code: Select all

  //you already have a TMyMetaData component named MyMetaData1 on the form
  MyMetaData1.MetaDataKind:='Tables';
  MyMetaData1.Restrictions.Values['TABLE_SCHEMA']:='Your_DataBase';
  MyMetaData1.Restrictions.Values['TABLE_NAME']:='Your_Table';
  MyMetaData1.Open;
  if MyMetaData1.RecordCount=0 then
    showmessage('Your_Tabl Not Exists')
  else
    showmessage('Your_Tabl Exists');

Re: How in MyDac to test is a table exists?

Posted: Thu 12 Dec 2013 05:06
by piedsoftware
Thanks, guys. Sorry about the belated reply.

In the end I went with this:
qry := CreateQry('show tables');
result := Qry.Locate(Qry.Fields[0].FieldName, tblName, [loCaseInsensitive]);
Qry.Free;

It was closer to what we had before.

---
Mark

Re: How in MyDac to test is a table exists?

Posted: Thu 12 Dec 2013 08:05
by AndreyZ
You can use such approach if it is more appropriate for you. Feel free to contact us if you have any further questions about MyDAC.