我对包含嵌套 SELECT 语句和 LEFT OUTER JOIN 语句的 UPDATE 语句有疑问。
情况:在我们的数据库中,向我们数据库中的所有人添加了一个新列,我想在某些人上更新值为“X”的列,但仅限于具有某些条件的人。
通过以下 SELECT 语句,我成功选择了所有需要更新此列的人。我要更新的列是“Testfield”
SELECT
TESTDB.CONTACT.number2 AS CustomerID,
TESTDB.PERSON.firstname AS FirstName,
TESTDB.PERSON.lastname AS LastName,
(SELECT UPPER(string19) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) AS Testfield
FROM
TESTDB.CONTACT
LEFT OUTER JOIN TESTDB.PERSON ON TESTDB.CONTACT.contact_id = TESTDB.PERSON.contact_id
LEFT OUTER JOIN TESTDB.UDPERSONSMALL ON TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.udpersonSmall_id
LEFT OUTER JOIN TESTDB.UDCONTACTLARGE ON TESTDB.CONTACT.userdef2_id = TESTDB.UDCONTACTLARGE.udcontactLarge_id
WHERE
(TESTDB.CONTACT.contact_id = TESTDB.PERSON.contact_id)
/* No retired people*/
AND TESTDB.PERSON.retired = '0'
/* Other conditions*/
AND ((SELECT UPPER(string05) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'A'
OR (SELECT UPPER(string07) FROM CRM5.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'B'
OR (SELECT UPPER(string08) FROM CRM5.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'C')
/* testpersonen*/
AND (SELECT string45 FROM TESTDB.UDPERSONLARGE WHERE TESTDB.PERSON.userdef2_id = TESTDB.UDPERSONLARGE.UDPERSONLARGE_id) = '123'
ORDER BY
CustomerID, FirstName, LastName
输出:
10001;Test1;Person1;
10001;Test2;Person2;
10001;Test3;Person3;
10001;Test4;Person4;
10001;Test6;Person6;
10001;Test7;Person7;
10001;Test8;Person8;
(7 rows affected)
这是正确的,人们目前在 Testfield 上的值为 NULL。(第四栏为空)
现在我想更新“Testfield”列,到目前为止尝试了这个:
UPDATE
TESTDB.CONTACT
LEFT OUTER JOIN TESTDB.PERSON ON TESTDB.CONTACT.contact_id = TESTDB.PERSON.contact_id
LEFT OUTER JOIN TESTDB.UDPERSONSMALL ON TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.udpersonSmall_id
LEFT OUTER JOIN TESTDB.UDCONTACTLARGE ON TESTDB.CONTACT.userdef2_id = TESTDB.UDCONTACTLARGE.udcontactLarge_id
SET
(SELECT UPPER(string19) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'J'
WHERE
(TESTDB.CONTACT.contact_id = TESTDB.PERSON.contact_id)
/* No retired people*/
AND TESTDB.PERSON.retired = '0'
/* Other conditions*/
AND ((SELECT UPPER(string05) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'A'
OR (SELECT UPPER(string07) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'B'
OR (SELECT UPPER(string08) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'C')
/* testpersonen*/
AND (SELECT string45 FROM TESTDB.UDPERSONLARGE WHERE TESTDB.PERSON.userdef2_id = TESTDB.UDPERSONLARGE.UDPERSONLARGE_id) = '123'
但是在这里我得到了它不接受放置在这里的 LEFT OUTER JOIN 的错误:
Msg 156, Level 15, State 1, Server TESTSERVER, Line 3
Incorrect syntax near the keyword 'LEFT'.
Msg 156, Level 15, State 1, Server TESTSERVER, Line 7
Incorrect syntax near the keyword 'SET'.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 7
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 15
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 16
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 17
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 19
Incorrect syntax near '='.
我也试过这样:
UPDATE
TESTDB
SET
(SELECT UPPER(string19) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'X'
FROM
TESTDB.CONTACT
LEFT OUTER JOIN TESTDB.PERSON ON TESTDB.CONTACT.contact_id = TESTDB.PERSON.contact_id
LEFT OUTER JOIN TESTDB.UDPERSONSMALL ON TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.udpersonSmall_id
LEFT OUTER JOIN TESTDB.UDCONTACTLARGE ON TESTDB.CONTACT.userdef2_id = TESTDB.UDCONTACTLARGE.udcontactLarge_id
WHERE
(TESTDB.CONTACT.contact_id = TESTDB.PERSON.contact_id)
/* No retired people*/
AND TESTDB.PERSON.retired = '0'
/* Other conditions*/
AND ((SELECT UPPER(string05) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'X'
OR (SELECT UPPER(string07) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'X'
OR (SELECT UPPER(string08) FROM TESTDB.UDPERSONSMALL WHERE TESTDB.PERSON.userdef_id = TESTDB.UDPERSONSMALL.UDPERSONSMALL_id) = 'X')
/* testpersonen*/
AND (SELECT string45 FROM TESTDB.UDPERSONLARGE WHERE TESTDB.PERSON.userdef2_id = TESTDB.UDPERSONLARGE.UDPERSONLARGE_id) = '123'
这导致另一个错误:
Msg 102, Level 15, State 1, Server TESTSERVER, Line 4
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 4
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 17
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 18
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 19
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Server TESTSERVER, Line 21
Incorrect syntax near '='.
该软件是一个带有 SQL Server 2005 数据库的 CRM。
有人可以帮我语法吗?我要迷路了。
提前致谢。