Delphi 7 - Detect whether a MSSQL table exists?

I need to determine whether a table exists inside a MSSQL server.
I.e.:
if Tabel1.Table.TableX.Exists then
//do Table1.Active
else
//create the table.

How can I do this?

Thanks.
John86aAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ephraim WangoyaCommented:

Use this function
Method 1
function TableExists(const ATableName: string): Boolean;
var
  Q: TADOQuery;
begin
  Q := TADOQuery.Create(nil);
  try
    Q.Connection := ADOConnection1;
    Q.SQL.Add('SELECT TABLE_NAME');
    Q.SQL.Add('FROM INFORMATION_SCHEMA.TABLES');
    Q.SQL.Add('WHERE TABLE_NAME = :TABLE_NAME');
    Q.Prepared := True; 

    Q.Parameters.ParmByName('TABLE_NAME').Value := ATableName;
    Q.Active := True;
    Result := not Q.IsEmpty;
    Q.Active := False
  finally
    FreeAndNil(Q);
  end;
end;
end;

Open in new window

0
Ephraim WangoyaCommented:

Method 2
function TableExists(const ATableName: string): Boolean;
var
  List: TStringList;
begin
  List := TStringList.Create;
  try
    ADOConnection1.GetTableNames(List);  
    Result := List.indexOf(ATableName) >= 0;
  finally
    FreeAndnil(List);
  end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ephraim WangoyaCommented:

My examples are based on ADO

So its up to you, with the query method, you can use any kind of dataset components that can run a query

The ADOConnection method gets you a list of all the tables so if you are goig to check a couple of tables, you can store the list in a global TStringList and just check against that list

If you are using different dataset componets, they should all be ably to do the same thing
type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    Button1: TButton;
    ADOQuery1: TADOQuery;
    procedure Button1Click(Sender: TObject);
  private
    function TableExists(const ATableName: string): Boolean;
    function TableExists2(const ATableName: string): Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.TableExists(const ATableName: string): Boolean;
var
  Q: TADOQuery;
begin
  Q := TADOQuery.Create(nil);
  try
    Q.Connection := ADOConnection1;
    Q.SQL.Add('SELECT TABLE_NAME');
    Q.SQL.Add('FROM INFORMATION_SCHEMA.TABLES');
    Q.SQL.Add('WHERE TABLE_NAME = :TABLE_NAME');
    Q.Prepared := True;

    Q.Parameters.ParamByName('TABLE_NAME').Value := ATableName;
    Q.Active := True;
    Result := not Q.IsEmpty;
    Q.Active := False
  finally
    FreeAndNil(Q);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if TableExists2('CABINET') then
    ShowMessage('yes');
end;

function TForm1.TableExists2(const ATableName: string): Boolean;
var
  List: TStringList;
begin
  List := TStringList.Create;
  try
    ADOConnection1.GetTableNames(List);  
    Result := List.indexOf(ATableName) >= 0;
  finally
    FreeAndnil(List);
  end;
end;

Open in new window

0
John86aAuthor Commented:
Nice, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.