Automation Anywhere SQL 个结果

Automation Anywhere SQL results

我正在尝试捕获我的 SQL 查询是否有 0 行或多行。如果它有 0 行然后我将插入,如果 1 将执行更新,如果 > 1 将执行附加分析。

有没有一种方法可以查看我的查询是在任何地方自动生成 x 个结果还是没有结果?

如有任何帮助,我们将不胜感激。

您可以使用 if existsif not exists 并在执行插入之前检查行是否存在,甚至是否存在多个行。

这是一个使用 if not exists 的简单示例,如果该行在 dbo.Table 上不存在,它将插入一行。如果它已经存在,那么 ID 将被记录到一个错误 table.

declare @InsertID int = 5, @Name nvarchar(max) = 'some name'
if ((select count(1) from dbo.Table where ID = @InsertID) > 1) -- detect error; more than one record for an id
begin
    insert into dbo.Error (ErrorID, ErrorDate)
    select @InsertID, getdate()
end
else if not exists (select 1 from dbo.Table where ID = @InsertID) -- no record exists for ID, insert it
begin
    insert into dbo.Table (ID, Name)
    select @InsertID, @Name
else if exists (select 1 from dbo.Table where ID = @InsertID)  -- update the single record
begin
    update dbo.Table set Name = @Name where ID = @InsertID 
end

A2019 returns SQL 查询的结果作为 table...

您可以在查询后立即使用 if 语句检查返回的行数 table 是否 > 0,然后采取相应的措施。