SQL - 子查询返回的值超过1。 - 多个数据库

时间:2016-12-20 04:47:29

标签: sql sql-server

我遇到以下更新声明的问题:

UPDATE tblPerson
SET personStatus = 'Candidate'
WHERE PersonID = 
(
    SELECT DISTINCT
        p.PersonnelID
    FROM 
        [Beyond-Auckland].dbo.Personnel p
    WHERE 
        EXISTS (SELECT ID FROM [Beyond-Auckland].dbo.LinkedDocuments L WHERE id = p.PersonnelId
                                                AND     SiteName IS NOT NULL
                                                AND     (Document LIKE ('%CV%') 
                                                OR      Document LIKE ('%resume%')
                                                OR      Document LIKE ('%Vitae%')
                                                OR      Document LIKE ('%Cover Letter%'))))

我一直收到以下错误:

  

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

不完全确定我在这里做错了什么,任何帮助都会非常感激

5 个答案:

答案 0 :(得分:0)

您无法使用=,因为它返回的值不止一个。将其更改为IN

UPDATE tblPerson
    SET personStatus = 'Candidate'
    WHERE PersonID IN 
    (
        SELECT DISTINCT
            p.PersonnelID
        FROM 
            [Beyond-Auckland].dbo.Personnel p
        WHERE 
            EXISTS (SELECT ID FROM [Beyond-Auckland].dbo.LinkedDocuments L WHERE id = p.PersonnelId
                                                    AND     SiteName IS NOT NULL
                                                    AND     (Document LIKE ('%CV%') 
                                                    OR      Document LIKE ('%resume%')
                                                    OR      Document LIKE ('%Vitae%')
                                                    OR      Document LIKE ('%Cover Letter%'))))

答案 1 :(得分:0)

检查这个。

            UPDATE tblPerson
            SET personStatus = 'Candidate'
            WHERE PersonID = 
            (
                SELECT DISTINCT
                    p.PersonnelID
                FROM 
                    [Beyond-Auckland].dbo.Personnel p
                WHERE 
                     PersonID IN (SELECT ID FROM [Beyond-Auckland].dbo.LinkedDocuments L WHERE id = p.PersonnelId
                                                            AND     SiteName IS NOT NULL
                                                            AND     (Document LIKE ('%CV%') 
                                                            OR      Document LIKE ('%resume%')
                                                            OR      Document LIKE ('%Vitae%')
                                                            OR      Document LIKE ('%Cover Letter%'))))

答案 2 :(得分:0)

使用IN运算符

UPDATE tblPerson
SET    personStatus = 'Candidate'
WHERE  PersonID IN (SELECT p.PersonnelID
                    FROM   [Beyond-Auckland].dbo.Personnel p
                    WHERE  EXISTS (SELECT ID
                                   FROM   [Beyond-Auckland].dbo.LinkedDocuments L
                                   WHERE  id = p.PersonnelId
                                          AND SiteName IS NOT NULL
                                          AND ( Document LIKE '%CV%' --Here
                                                 OR Document LIKE '%resume%'
                                                 OR Document LIKE '%Vitae%'
                                                 OR Document LIKE '%Cover Letter%' ))) 

您还需要正确添加括号apply OR运算符。 AND的优先级高于OR运算符,因此首先会对其进行评估

您当前的Where子句将按此处理

WHERE  id = p.PersonnelId
       AND ( SiteName IS NOT NULL AND Document LIKE '%CV%' )
        OR Document LIKE '%resume%'
        OR Document LIKE '%Vitae%'
        OR Document LIKE '%Cover Letter%' 

答案 3 :(得分:0)

使用Simply JOIN代替IN和EXISTS,如下所示:

UPDATE tp SET personStatus = 'Candidate'
FROM  tblPerson tp
INNER JOIN [Beyond-Auckland].dbo.Personnel p ON tp.PersonID = p.PersonnelID
INNER JOIN [Beyond-Auckland].dbo.LinkedDocuments L ON id = p.PersonnelId
    AND SiteName IS NOT NULL
    AND (Document LIKE ('%CV%') OR  Document LIKE ('%resume%') OR Document LIKE ('%Vitae%') OR Document LIKE ('%Cover Letter%'))

答案 4 :(得分:0)

Directly update from table 2 to table 1 :

UPDATE tblPerson SET personStatus = 'Candidate'
FROM [Beyond-Auckland].dbo.Personnel P
WHERE p.PersonnelID = tblPerson.PersonID AND 
EXISTS
 (
    SELECT 1 FROM [Beyond-Auckland].dbo.LinkedDocuments L WHERE id =   
    p.PersonnelId  AND SiteName IS NOT NULL  AND 
  (
   Document LIKE ('%CV%') OR
   Document LIKE ('%resume%') OR      
   Document LIKE ('%Vitae%') OR
   Document LIKE ('%Cover Letter%')
  )
)