【发布时间】:2011-07-08 12:00:21
【问题描述】:
在 SQL Server 中,您可以编写 SQL 来检查表是否存在。我怎样才能为 ADS 做到这一点?
我需要编写一些 Delphi 代码来说明表是否存在,否则执行此操作...
【问题讨论】:
标签: delphi sybase advantage-database-server
在 SQL Server 中,您可以编写 SQL 来检查表是否存在。我怎样才能为 ADS 做到这一点?
我需要编写一些 Delphi 代码来说明表是否存在,否则执行此操作...
【问题讨论】:
标签: delphi sybase advantage-database-server
系统过程sp_GetTables可以告诉你在你连接的目录中存在哪些表:
执行过程 sp_GetTables(NULL, NULL, NULL, 'TABLE')
非 SQL 解决方案是使用 AdsCheckExistence API。
【讨论】:
我不是ADS用户,所以无法详细回答。
见http://devzone.advantagedatabase.com/dz/webhelp/Advantage10.1/index.html
这是 system.tables 视图,其中包含有关表的信息。 我想你也可以编写 SQL 查询来检查一个表。
【讨论】:
我喜欢 Peter 的回答,但根据您需要做什么,您可能正在寻找 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;
【讨论】:
德尔福代码:
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
结束;
【讨论】: