복붙노트

[SQL] INNER는 DB2에 대한 업데이트는 SQL에 가입하세요

SQL

INNER는 DB2에 대한 업데이트는 SQL에 가입하세요

사용 DB2에 대한 업데이트 문에 조인하는 방법은 무엇입니까?

구글은 정말이 일에 나를 실망했다

이 대략 무엇인지 내가 달성하기 위해 노력하고있어 (분명 작업을 제외하고 ... ...)

update file1 inner join file2                                 
       on substr(file1.firstfield,10,20) = substr(file2.anotherfield,1,10)                                                                    
set file1.firstfield = ( 'BIT OF TEXT' concat file2.something )                                                                             
where file1.firstfield like 'BLAH%'                             

건배

해결법

  1. ==============================

    1.당신은 당신이 타겟팅 어떤 플랫폼 말을하지 않습니다. 파일로 테이블을 참조하지만, 리드 나 리눅스, UNIX 또는 Windows (LUW)에 DB2를 실행하지 않을 것으로 생각합니다.

    당신은 당신이 타겟팅 어떤 플랫폼 말을하지 않습니다. 파일로 테이블을 참조하지만, 리드 나 리눅스, UNIX 또는 Windows (LUW)에 DB2를 실행하지 않을 것으로 생각합니다.

    당신은 DB2 LUW에있는 경우, MERGE 문을 참조하십시오

    귀하의 예를 문의 경우,이는 다음과 같이 기록 될 것입니다 :

    merge into file1 a
       using (select anotherfield, something from file2) b
       on substr(a.firstfield,10,20) = substr(b.anotherfield,1,10)
    when matched and a.firstfield like 'BLAH%'
       then update set a.firstfield = 'BIT OF TEXT' || b.something;
    

    제발 참고 : DB2를 들어, SUBSTR 함수의 세 번째 인수는 반환 바이트의 수,하지 끝 위치입니다. 따라서, SUBSTR (a.firstfield, 10, 20)는 CHAR (20)을 반환합니다. 그러나, SUBSTR (b.anotherfield, 1,10) CHAR (10)을 반환합니다. 이 목적에 이루어졌다 있는지 확실하지 않습니다, 그러나 당신의 비교에 영향을 미칠 수 있습니다.

  2. ==============================

    2.업데이트 문에 조인이 아닌 표준 및 모든 공급 업체가 지원되지 않습니다. 당신이 할하려는하는 것은으로 수행 할 수 있습니다 하위 선택 :

    업데이트 문에 조인이 아닌 표준 및 모든 공급 업체가 지원되지 않습니다. 당신이 할하려는하는 것은으로 수행 할 수 있습니다 하위 선택 :

    update
      file1
    set
      firstfield = (select 'stuff' concat something from file2 where substr(file1.field1, 10, 20) = substr(file2.xxx,1,10) )
    where
      file1.foo like 'BLAH%'
    
  3. ==============================

    3.나에게 결과를 알려 다음이 시도하고 :

    나에게 결과를 알려 다음이 시도하고 :

    UPDATE File1 AS B                          
    SET    b.campo1 = (SELECT DISTINCT A.campo1
                       FROM  File2 A           
                       INNER JOIN File1      
                       ON A.campo2 = File1.campo2 
                       AND A.campo2 = B.campo2) 
    
  4. ==============================

    4.답 https://stackoverflow.com/a/4184237/565525로 업데이트 :

    답 https://stackoverflow.com/a/4184237/565525로 업데이트 :

    여러 열을 원하는 경우, 그이 같은 achived 할 수 있습니다 :

    update file1
    set
      (firstfield, secondfield) = (
            select 'stuff' concat 'something from file2', 
                   'some secondfield value' 
            from file2
            where substr(file1.field1, 10, 20) = substr(file2.xxx,1,10) )
    where
      file1.foo like 'BLAH%'
    

    출처 : http://www.dbforums.com/db2/1615011-sql-update-using-join-subquery.html#post6257307

  5. ==============================

    5.여기에 그냥 작업을 가지고 무언가의 좋은 예이다 :

    여기에 그냥 작업을 가지고 무언가의 좋은 예이다 :

    update cac c
    set ga_meth_id = (
        select cim.ga_meth_id 
        from cci ci, ccim cim 
        where ci.cus_id_key_n = cim.cus_id_key_n
        and ci.cus_set_c = cim.cus_set_c
        and ci.cus_set_c = c.cus_set_c
        and ci.cps_key_n = c.cps_key_n
    )
    where exists (
        select 1  
        from cci ci2, ccim cim2 
        where ci2.cus_id_key_n = cim2.cus_id_key_n
        and ci2.cus_set_c = cim2.cus_set_c
        and ci2.cus_set_c = c.cus_set_c
        and ci2.cps_key_n = c.cps_key_n
    )
    
  6. ==============================

    6.그냥 다른 행만을 조건과 일치 행 및 회피 업데이트 널 (null)을 업데이트합니다 :

    그냥 다른 행만을 조건과 일치 행 및 회피 업데이트 널 (null)을 업데이트합니다 :

    update table_one set field_1 = 'ACTIVE' where exists 
    (select 1 from table_two where table_one.customer = table_two.customer);
    

    그것은 DB2에서 작동 / AIX64 9.7.8

  7. ==============================

    7.DB2 LUW 9.7에 UPDATE 문에 대한 참조 문서는 다음과 같은 예제를 제공합니다 :

    DB2 LUW 9.7에 UPDATE 문에 대한 참조 문서는 다음과 같은 예제를 제공합니다 :

       UPDATE (SELECT EMPNO, SALARY, COMM,
         AVG(SALARY) OVER (PARTITION BY WORKDEPT),
         AVG(COMM) OVER (PARTITION BY WORKDEPT)
         FROM EMPLOYEE E) AS E(EMPNO, SALARY, COMM, AVGSAL, AVGCOMM)
       SET (SALARY, COMM) = (AVGSAL, AVGCOMM)
       WHERE EMPNO = '000120'
    

    UPDATE 후 괄호가 갈 수있는 유효한 SELECT 문을 의미 전체가-선택하여 포함 할 수 있습니다.

    그 바탕으로, 나는 다음을 제안합니다 :

    UPDATE (
      SELECT
        f1.firstfield,
        f2.anotherfield,
        f2.something
      FROM file1 f1
      WHERE f1.firstfield like 'BLAH%' 
      INNER JOIN file2 f2
      ON substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
    )
    AS my_files(firstfield, anotherfield, something)
    SET
      firstfield = ( 'BIT OF TEXT' || something )
    

    편집 : 이안 권리입니다. 나의 첫번째 본능 대신 부속 선택을 시도했다 :

    UPDATE file1 f1
    SET f1.firstfield = ( 'BIT OF TEXT' || (
      SELECT f2.something
      FROM file2 f2
      WHERE substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
    ))
    WHERE f1.firstfield LIKE 'BLAH%' 
    AND substr(f1.firstfield,10,20) IN (
      SELECT substr(f2.anotherfield,1,10)
      FROM file2 f2
    )
    

    그러나 나는 연결이 일하는 것이 있는지 확실하지 않습니다. 하위 문자열 사이 : 1 매핑 그것은 또한 1가 있다고 가정합니다. 여러 행이있는 경우 일치, 그것은 작동하지 않을 것이다.

  8. ==============================

    8.이 질문에 대한

    이 질문에 대한

    update file1 f1
    set file1.firstfield=
    (
    select 'BIT OF TEXT' || f2.something
    from file2 f2
    where substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
    )
    where exists
    (
    select * from file2 f2
    where substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
    )
    and f1.firstfield like 'BLAH%'
    

    여러 결과를 줄에 가입하는 경우는 다음과 같이 갱신을 강제 할 수

    update file1 f1
    set file1.firstfield=
    (
    select 'BIT OF TEXT' || f2.something
    from file2 f2
    where substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
    fetch first rows only
    )
    where exists
    (
    select * from file2 f2
    where substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
    )
    and f1.firstfield like 'BLAH%' 
    

    템플릿 방법

    update table1 f1
    set (f1.field1, f1.field2, f1.field3, f1.field4)=
    (
    select f2.field1, f2.field2, f2.field3, 'CONSTVALUE'
    from table2 f2
    where (f1.key1, f1.key2)=(f2.key1, f2.key2) 
    )
    where exists 
    (
    select * from table2 f2
    where (f1.key1, f1.key2)=(f2.key1, f2.key2)
    ) 
    
  9. ==============================

    9.표준 SQL에서는 업데이트 외모의이 유형처럼 :

    표준 SQL에서는 업데이트 외모의이 유형처럼 :

    update a
       set a.firstfield ='BIT OF TEXT' + b.something
      from file1 a
      join file2 b
        on substr(a.firstfield,10,20) = 
           substr(b.anotherfield,1,10)
     where a.firstfield like 'BLAH%' 
    

    (I 시험에 손에 DB / 2 인스턴스를 가지고 있지 않지만) 작은 통 사적 변화와 함께 것은 이런 종류의 DB / 2 것이다 거의 확실하게 작업을 Oracle 또는 SQL Server에서 일을하고 있습니다.

  10. from https://stackoverflow.com/questions/4184209/inner-join-in-update-sql-for-db2 by cc-by-sa and MIT license