如何使用变量更新行集

时间:2014-07-08 19:54:14

标签: sql sql-server

我对SQL很新,我正在尝试编写一个更新语句,它实质上将数据从一列复制到另一列600条记录。

离。 ADDRESS.FIRST_NAME 即。" JOHN"将复制在 ADDRESS.CUSTCHAR5 " JOHN"所有600条记录。我有四个列需要发生。这是我已经得到的。

  

DECLARE @fName CHAR,@ list CHAR,@ email CHAR,@ phoneNum CHAR
SET @fName =(选择FIRST_NAME来自ADDRESS INNER JOIN CUST   ON ADDRESS.CUST_CODE = CUST.CUST_CODE在哪里CUST.CATEGORY_CODE =   ' SR' AND ADDRESS.CUSTCHAR5为空)
SET @lName =(SELECT   来自ADDRESS INNER JOUST的LAST_NAME必须在ADDRESS.CUST_CODE =   CUST.CUST_CODE CUST.CATEGORY_CODE =' SR' AND ADDRESS.CUSTCHAR5   IS NULL)
SET @email =(从地址内部选择电子邮件   JOUST CUST ON ADDRESS.CUST_CODE = CUST.CUST_CODE WHERE   CUST.CATEGORY_CODE =' SR' AND ADDRESS.CUSTCHAR5是NULL)   
SET @phoneNum =(选择TEL1来自地址内部联接CUST ON   ADDRESS.CUST_CODE = CUST.CUST_CODE CUST.CATEGORY_CODE =' SR'和   ADDRESS.CUSTCHAR5为空)
UPDATE dbo.ADDRESS   
SET ADDRESS.CUSTCHAR5 = @ fName + @ lName,ADDRESS.CUSTCHAR6 =   @email,ADDRESS.CUSTMEMO1 = @phoneNum
FROM ADDRESS   
INNER JOIN CUST
ON ADDRESS.CUST_CODE = CUST.CUST_CODE   
WHERE CUST.CATEGORY_CODE =' SR'
AND ADDRESS.CUSTCHAR5   IS NULL为 AND ADDRESS.EMAIL = @email

当我运行此语句时,我收到以下错误

  

子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   表达。

我很感激任何人都可以提供帮助。

1 个答案:

答案 0 :(得分:2)

为SQL Server尝试类似的操作。

UPDATE dbo.ADDRESS
SET 
    ADDRESS.CUSTCHAR5 = FIRST_NAME + LAST_NAME,
    ADDRESS.CUSTCHAR6 = EMAIL,
    ADDRESS.CUSTMEMO1 = TEL1 
FROM CUST 
WHERE ADDRESS.CUST_CODE = CUST.CUST_CODE
    AND CUST.CATEGORY_CODE = 'SR'
    AND ADDRESS.CUSTCHAR5 IS NULL
    AND ADDRESS.EMAIL = @email