在 SQL Server 中,您可以编写 SQL 来检查表是否存在。我怎样才能为 ADS 做到这一点?
我需要编写一些Delphi代码来说明表是否存在,否则执行此操作...
在 SQL Server 中,您可以编写 SQL 来检查表是否存在。我怎样才能为 ADS 做到这一点?
我需要编写一些Delphi代码来说明表是否存在,否则执行此操作...
系统过程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 查询来检查表。
我喜欢彼得的回答,但根据您需要做什么,您可能正在寻找 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
结尾;