使用DB2 MERGE ::错误代码:-788,SQL状态:21506

时间:2014-10-07 21:08:03

标签: sql merge sql-update db2

要求:

输入表: csscustomertest< CUSTOMERCID是PK> ,CHILDDETAILS< CUSTOMERCID是FK>

输入: CHILDDETAILS.FIRSTNAME:玛丽

输出: CHILDDETAILS.FIRSTNAME:Mchildfirst0215

其中0215是来自csscustomertest

的出生日期(MMDD)

我已使用此语句更新CHILDDETAILS

merge into CHILDDETAILS 
  using ( 
    select 
      CHILDDETAILS.CUSTOMERCID,
      concat(
        trim(concat(substr(coalesce( CHILDDETAILS.FIRSTNAME,''),1,1), 'childfirst')),
        concat(
          trim(coalesce(substr( cast(csscustomertest.dob as char(10) ), 6,2),'Mx')),
          trim(coalesce(substr( cast(csscustomertest.dob as char(10) ), 9,2),'Dx'))                
        )
      )   as    childfirst     
    FROM 
      csscustomertest 
    inner join 
      CHILDDETAILS  
    on ( csscustomertest.CUSTOMERCID = CHILDDETAILS.CUSTOMERCID )  
  )  CUST 
on ( CUST.CUSTOMERCID = CHILDDETAILS.CUSTOMERCID )  
when matched   then 
  update set CHILDDETAILS.FIRSTNAME=CUST.childfirst

出现此错误:

  

[错误代码:-788,SQL状态:21506]目标表的同一行   " DB2ADMIN.CHILDDETAILS"为了更新,不止一次被识别出来   删除或插入MERGE语句的操作.. SQLCODE = -788,   SQLSTATE = 21506,DRIVER = 3.57.82

还尝试使用此查询:

UPDATE 
  CHILDDETAILS 
SET 
  FIRSTNAME=(
    select 
      concat(
        trim(concat(substr(coalesce( CHILDDETAILS.FIRSTNAME,''),1,1), 'childfirst')),
        concat(
          trim(coalesce(substr( cast(csscustomertest.dob as char(10) ), 6,2),'Mx')),
          trim(coalesce( substr( cast(csscustomertest.dob as char(10) ), 9,2),'Dx'))                
        )
      )   as    childfirst   
    FROM 
      csscustomertest 
    inner join 
      CHILDDETAILS  
    on ( csscustomertest.CUSTOMERCID = CHILDDETAILS.CUSTOMERCID ) 
    WHERE 
      CHILDDETAILS.CUSTOMERCID=220833
  ) 
WHERE  
  CHILDDETAILS.CUSTOMERCID=220833;  

得到类似的错误:

  

[错误代码:-811,SQL状态:21000]标量的结果   全选,SELECT INTO语句或VALUES INTO语句更多   比一行.. SQLCODE = -811,SQLSTATE = 21000,DRIVER = 3.57.82

我想我无法在上面的查询中更新同一个表,可能是我需要编写一个游标。感谢任何建议。

2 个答案:

答案 0 :(得分:1)

首先,merge语句是这里的方式。

您的问题是更新的源查询(using(...)内的查询)返回的行数多为CHILDDETAILS.CUSTOMERCID

每个客户CHILDDETAILScsscustomertest都有多行。

如果复制位于DISTINCT,您可以在源查询上使用csscustomertest

如果复制在CHILDDETAILS,您可能需要使用group by并提出一个规则,您可以选择详细信息。但是,我认为CHILDDETAILS中的重复行可能会成为您需要解决的数据的问题。

答案 1 :(得分:0)

感谢您及时投入。我能够解决问题。

上述问题的原因: csscustomertest和CHILDDETAILS表之间的一对多关系。

解决方案:在查询中添加了以下条款。

合并到CHILDDETAILS    使用(                选择 CHILDDETAILS.CHILDCID ,CHILDDETAILS.CUSTOMERCID,CHILDDETAILS.FIRSTNAME,CHILDDETAILS.LASTNAME,             CONCAT(                              trim(concat(substr(coalesce(CHILDDETAILS.FIRSTNAME,''),1,1),'childfirst')),                             CONCAT(                                                     trim(coalesce(substr(cast(csscustomertest.dob as char(10)),6,2),'Mx')),                                                     trim(coalesce(substr(cast(csscustomertest.dob as char(10)),9,2),'Dx'))
                                          )                    )作为孩子的第一个            从csscustomertest内部加入CHILDDETAILS(csscustomertest.CUSTOMERCID = CHILDDETAILS.CUSTOMERCID)
                   其中CHILDDETAILS.CUSTOMERCID = 220833          )CUST      on(CUST.CUSTOMERCID = CHILDDETAILS.CUSTOMERCID 和CUST.CHILDCID = CHILDDETAILS.CHILDCID

匹配时

AND CUST.CUSTOMERCID = 220833    然后更新集CHILDDETAILS.FIRSTNAME = CUST.childfirst;

<强>代码:

merge into CHILDDETAILS 
   using ( 
           select CHILDDETAILS.CHILDCID, CHILDDETAILS.CUSTOMERCID,CHILDDETAILS.FIRSTNAME,CHILDDETAILS.LASTNAME,
        concat(
                         trim(concat(substr(coalesce( CHILDDETAILS.FIRSTNAME,''),1,1), 'childfirst')),
                        concat(
                                                trim(coalesce(substr( cast(csscustomertest.dob as char(10) ), 6,2),'Mx')),
                                                trim(coalesce( substr( cast(csscustomertest.dob as char(10) ), 9,2),'Dx'))                
                                      )
               )   as    childfirst    
       FROM csscustomertest inner join CHILDDETAILS  on ( csscustomertest.CUSTOMERCID = CHILDDETAILS.CUSTOMERCID )  
               where  CHILDDETAILS.CUSTOMERCID=220833
     )  CUST 
 on ( CUST.CUSTOMERCID = CHILDDETAILS.CUSTOMERCID and  CUST.CHILDCID=CHILDDETAILS.CHILDCID)  

when matched  AND CUST.CUSTOMERCID=220833
   then update set CHILDDETAILS.FIRSTNAME=CUST.childfirst;