【问题标题】:SQL - subquery is not introduced with EXISTSSQL - EXISTS 不引入子查询
【发布时间】:2016-09-13 18:23:30
【问题描述】:

我正在尝试更新除第一个(最低)帐号之外的所有 creditCounts,但是我一直收到此错误:

消息 116,第 16 级,状态 1,第 8 行
当子查询不使用 EXISTS 引入时,选择列表中只能指定一个表达式。

这里是查询:

update Recog
set livesCount = 0
where RECID in (select 
                    r.empNumber, r.acctNbr, r.creditCount, r.groupAcctNumber, r.groupType
                from 
                    Recog r
                where 
                    creditCount > 0 
                    and policyNbr in 
                        (
                            (Select acctNbr from Recog)
                            except 
                            (Select MIN(acctNbr) 
                             from Recog 
                             Group By groupAcctNumber, groupType)
                            )
                        )

我该如何解决?

【问题讨论】:

  • 你说的是Where RECID in...,然后给它5个字段。 in时只能选择ONE。

标签: sql sql-server


【解决方案1】:

由于错误状态,您在子查询中选择了多个列,并且只选择一个才有意义......

update Recog
    set livesCount = 0
    where RECID in (
    select r.RECID
    from Recog r
          where creditCount > 0 and
          policyNbr in ((Select acctNbr from Recog)
          except (Select MIN(acctNbr) from Recog Group By groupAcctNumber,groupType)))

为了把它带回家,想象一下如果你用列数据代替你的子选择,你的语句会是什么样子......

update Recog
    set livesCount = 0
    where RECID in (1, 'Account 123', 12, 'Group 123', 'Type X') -- Makes no sense

【讨论】:

    猜你喜欢
    • 2016-06-12
    • 2016-02-23
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2016-07-22
    • 2011-08-05
    相关资源
    最近更新 更多