SQL Server Subquery returned more than 1 value. This is not permitted when the subquery follows (chars) or when the subquery is used as an expression

后端 未结 3 988
眼角桃花
眼角桃花 2021-02-19 20:14

I am trying to update some fields based on their occurence. If they only occur one time, I am updating some status field.

My current code is as follows:

         


        
3条回答
  •  一生所求
    2021-02-19 20:38

    You should join your tables in the subselect. It is possible to use 'in', but in your case I would use exists:

    UPDATE table1 x
    SET statusField = 1
    WHERE exists (
                   SELECT null
                   FROM table1
                   WHERE x.someID = someID
                   GROUP BY someID 
                   HAVING COUNT(*) = 1
                   )
    

    For better performance I would use this script instead (sqlserver-2008+):

    ;WITH x as
    (
    SELECT rc = count() over (partition by someID), statusField
    FROM table1
    )
    UPDATE x
    SET statusField = 1
    WHERE rc = 1
    

提交回复
热议问题