检测表是否存在

发布于 2024-11-18 20:49:33 字数 108 浏览 2 评论 0原文

在 SQL Server 中,您可以编写 SQL 来检查表是否存在。我该如何为 ADS 做到这一点?

我需要编写一些 Delphi 代码来说明如果表存在则执行此操作,否则执行此操作...

In SQL Server you can write SQL to check if a table exists. How can I do that for ADS?

I have a need to write some Delphi code to say if table exists do this else this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虚拟世界 2024-11-25 20:49:33

系统过程sp_GetTables可以告诉你哪些表存在于您连接到的目录中:

EXECUTE PROCEDURE sp_GetTables( NULL, NULL, NULL, 'TABLE' )

非 SQL 解决方案是使用 AdsCheckExistence API。

The system procedure sp_GetTables can tell you what tables exist in the directory that you connected to:

EXECUTE PROCEDURE sp_GetTables( NULL, NULL, NULL, 'TABLE' )

A non-SQL solution would be to use the AdsCheckExistence API.

眸中客 2024-11-25 20:49:33

我不是ADS用户,所以无法详细回答。

请参阅 http://devzone.advantagedatabase.com/dz/webhelp/Advantage10。 1/index.html

这是 system.tables 视图,其中包含有关表的信息。
我想你也可以编写 SQL 查询来检查表。

I'm not ADS user, so I can't answer in detail.

See http://devzone.advantagedatabase.com/dz/webhelp/Advantage10.1/index.html

The're is system.tables view with information about tables.
I suppose you also can write SQL query to check a table.

倒数 2024-11-25 20:49:33

Delphi代码:

function TableExists(AConnection: TADOConnection; const TableName: string): boolean;
var
R: _Recordset;
begin
if AConnection.Connected then
try
  R := AConnection.Execute('Select case when OBJECT_ID(''' + TableName + ''',''U'') > 0 then 1 else 0 end as [Result]', cmdText, []);
  if R.RecordCount > 0 then
   Result := (R.Fields.Items['Result'].Value = 1);

except on E:exception do Result := false;
end;

这个简单的函数使用现有的TADOConnection

端;

Delphi code:

function TableExists(AConnection: TADOConnection; const TableName: string): boolean;
var
R: _Recordset;
begin
if AConnection.Connected then
try
  R := AConnection.Execute('Select case when OBJECT_ID(''' + TableName + ''',''U'') > 0 then 1 else 0 end as [Result]', cmdText, []);
  if R.RecordCount > 0 then
   Result := (R.Fields.Items['Result'].Value = 1);

except on E:exception do Result := false;
end;

this simple function use existing TADOConnection

end;

简单气质女生网名 2024-11-25 20:49:33

我喜欢彼得的回答,但根据您需要做什么,您可能正在寻找 TRY、CATCH、FINALLY 语句。

TRY
   // Try to do something with the table
   select top 1 'file exists' from "non_existing_table";

CATCH ADS_SCRIPT_EXCEPTION 

   // If a "7041 - File does not exist" error ocurrs
   IF __errcode = 7041 THEN 
      // Do something else
      select 'file does not exist' from system.iota; 

   ELSE 
      // re-raise the other exception
      RAISE; 

   END IF; 
END TRY;

I like Peter's answer, but depending on what it is you need to do, you might be looking for a TRY, CATCH, FINALLY statement.

TRY
   // Try to do something with the table
   select top 1 'file exists' from "non_existing_table";

CATCH ADS_SCRIPT_EXCEPTION 

   // If a "7041 - File does not exist" error ocurrs
   IF __errcode = 7041 THEN 
      // Do something else
      select 'file does not exist' from system.iota; 

   ELSE 
      // re-raise the other exception
      RAISE; 

   END IF; 
END TRY;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文