【问题标题】:Automation Anywhere SQL resultsAutomation Anywhere SQL 结果
【发布时间】:2020-08-22 02:32:52
【问题描述】:

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

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

我们将不胜感激。

【问题讨论】:

  • 由于您的问题不清楚,请提供您的查询、示例数据和预期结果。

标签: sql sql-server resultset is-empty automationanywhere


【解决方案1】:

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

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

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

【讨论】:

  • 非常感谢您的回复。只有我们的 DBA 才能创建存储过程;但是,我以另一种方式使用了您建议的解决方案,并且有效!我对表的 id 进行了计数,并计算了返回的行数。然后我将 $Dataset Column(1)$ 分配给一个变量,然后对 $Dataset Column(1)$ 进行循环,然后执行 If 、 Else If、 Else 以检查结果是否为 0,1 或更大。谢谢你!感谢您激发我解决的想法。向你致敬!
  • @Murita,我更新了答案,所以它只是一个脚本而不是存储过程。让我知道您是否有任何问题或这是否适用于答案。
【解决方案2】:

A2019 将 SQL 查询的结果作为表返回...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多