优化此UPDATE语句以不使用游标

时间:2011-05-06 21:48:32

标签: sql db2 sql-update

FOR v2 AS
    c2 CURSOR FOR
        SELECT he.MyPrimary, he.SomeCode, he.SomeName, pe.MyPrimary
        FROM SomeTable he
            INNER JOIN AnotherTable pe
                ON (he.ColOne = pe.FooOne
                    AND he.ColTwo = pe.ColTwo
                    AND he.ColThree = pe.FooOne
                    AND he.SomeCode = pe.SomeCode)
        WHERE he.relevancy = 1 AND he.ColThree = '2011-01-05' AND he.ColFive = 9
DO
    UPDATE AnotherTable SET match = he.MyPrimary, FooTwo = he.SomeCode, SomeName = he.SomeName WHERE MyPrimary = pe.MyPrimary;  

END FOR;

我有上面的代码,我试图在不使用游标的情况下执行此操作,但我不确定如何使用UPDATE执行INNER JOIN语句。基本上,我想要做的是加入两个表SomeTableAnotherTable然后根据SomeTable中的一些列值,将值复制到AnotherTable中的类似列。我正在使用DB2。

编辑:我只是在调查:INNER JOIN in UPDATE sql for DB2

做这样的事情是否有意义:

UPDATE
  SomeTable pe
SET
  match = (SELECT he.MyPrimary FROM SomeTable he WHERE he.ColOne = pe.FooOne
                            AND he.ColTwo = pe.ColTwo
                            AND he.ColThree = pe.FooOne
                            AND he.SomeCode = pe.SomeCode ),

  FooTwo = (SELECT he.SomeCode FROM SomeTable he WHERE he.ColOne = pe.FooOne
                            AND he.ColTwo = pe.ColTwo
                            AND he.ColThree = pe.FooOne
                            AND he.SomeCode = pe.SomeCode )
WHERE
  he.relevancy = 1 AND he.ColThree = '2011-01-05' AND he.ColFive = 9

2 个答案:

答案 0 :(得分:1)

正如您提到的链接一样,ISO / ANSI标准不允许在子语句中使用它之外的Update语句中的连接。因此,您必须执行多个Update语句,或者为每个列执行子查询。

Update AnotherTable 
Set match = (
                Select he.MyPrimary
                From SomeTable he
                Where he.ColOne = AnotherTable.FooOne
                    And he.ColTwo = AnotherTable.ColTwo
                    And he.ColThree = AnotherTable.FooOne
                    And he.SomeCode = AnotherTable.SomeCode
                    And he.relevancy = 1 
                    And he.ColThree = '2011-01-05' 
                    And he.ColFive = 9
                )
    , FooTwo =  (
                Select he.SomeCode
                From SomeTable he
                Where he.ColOne = AnotherTable.FooOne
                    And he.ColTwo = AnotherTable.ColTwo
                    And he.ColThree = AnotherTable.FooOne
                    And he.SomeCode = AnotherTable.SomeCode
                    And he.relevancy = 1 
                    And he.ColThree = '2011-01-05' 
                    And he.ColFive = 9
                )
    , SomeName =    (
                    Select he.SomeName
                    From SomeTable he
                    Where he.ColOne = AnotherTable.FooOne
                        And he.ColTwo = AnotherTable.ColTwo
                        And he.ColThree = AnotherTable.FooOne
                        And he.SomeCode = AnotherTable.SomeCode
                        And he.relevancy = 1 
                        And he.ColThree = '2011-01-05' 
                        And he.ColFive = 9
                    )
Where Exists    (
                Select 1
                From SomeTable he
                Where he.ColOne = AnotherTable.FooOne
                    And he.ColTwo = AnotherTable.ColTwo
                    And he.ColThree = AnotherTable.FooOne
                    And he.SomeCode = AnotherTable.SomeCode
                    And he.relevancy = 1 
                    And he.ColThree = '2011-01-05' 
                    And he.ColFive = 9
                )

答案 1 :(得分:0)

有一种更好的方法可以做到这一点;

UPDATE SomeTable pe SET (match, FooTwo, SomeName) = (SELECT he.MyPrimary, he.SomeCode, he.SomeName
                                                     FROM AnotherTable he 
                                                     WHERE he.ColOne = pe.FooOne
                                                     AND he.ColTwo = pe.ColTwo
                                                     AND he.ColThree = pe.FooOne
                                                     AND he.SomeCode = pe.SomeCode)
WHERE he.relevancy = 1 
AND he.ColThree = '2011-01-05' 
AND he.ColFive = 9

这在DB2的iSeries版本上运行良好 如果您需要担心NULL行,请不要忘记您的exists子句:

AND EXISTS (SELECT '1'
            FROM AnotherTable he
            WHERE he.ColOne = pe.FooOne
            AND he.ColTwo = pe.ColTwo
            AND he.ColThree = pr.FooOne
            AND he.SomeCode = pe.SomeCode)  

在主WHERE语句中的现有UPDATE子句之后添加。