在更新表时使用返回的选择行

时间:2017-08-02 06:57:52

标签: sql oracle

我必须根据条件更新选定的行。 以下查询返回几行:

select
        b.CRDATTIM,
        b.RECORDCD,
        b.CRNODE,
        b.UNITCD,
        b.WRKTYPE


    from W67U999S a
    join W03U999S b
        on a.CCRDATTIM = b.CRDATTIM and a.CRECORDCD = b.RECORDCD and a.CCRNODE = b.CRNODE
    where a.PCRDATTIM = '2017-07-20-04.22.20.893340'
    and a.PRECORDCD = 'F'
    and a.PCRNODE = '01'
    and b.WRKTYPE = 'CALLER'
    and b.UNITCD='CS2XAA';

现在,我必须根据以下pcrdattim条件更新上述W67u999s查询中已使用的select表格的where

W67u999s.ccrdattim=returned select row.crdattim

我尝试过以下查询,但它无效。:

update W67u999s set pcrdattim='2017-07-31-07.40.42.355440' from W67U999S as a inner join W03U999S as b on a.CCRDATTIM = b.CRDATTIM and a.CRECORDCD = b.RECORDCD and a.CCRNODE = b.CRNODE
    where a.PCRDATTIM = '2017-07-20-04.22.20.893340'
    and a.PRECORDCD = 'F'
    and a.PCRNODE = '01'
    and b.WRKTYPE = 'CALLER'
    and b.UNITCD='CS2XAA'
    and a.ccrdattim=crdattim and b.crecordcd='T';

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

在这种情况下,

MERGE是你的朋友。 MERGE允许您只使用JOIN(就像您已有的那样),并将其用作桌面上UPDATE的基础。

MERGE需要从USING子句到要更新的表的连接。在大多数用例中,使用主键就可以了。由于您没有提供完整的表结构,请参阅此示例并根据您的需要进行调整。

merge into W67U999S t 
using (

    select
        a.primary_key, b.CRDATTIM -- change this line

        /*
            b.CRDATTIM,
            b.RECORDCD,
            b.CRNODE,
            b.UNITCD,
            b.WRKTYPE
        */

        from W67U999S a
        join W03U999S b
            on a.CCRDATTIM = b.CRDATTIM and a.CRECORDCD = b.RECORDCD and a.CCRNODE = b.CRNODE
        where a.PCRDATTIM = '2017-07-20-04.22.20.893340'
        and a.PRECORDCD = 'F'
        and a.PCRNODE = '01'
        and b.WRKTYPE = 'CALLER'
        and b.UNITCD='CS2XAA'
) u
on (t.primary_key = u.primary_key)
when matched then update set t.pcrdattim = u.CRDATTIM

答案 1 :(得分:0)

以下查询对我有用(exists在这里发挥了作用):

update W67U999S a set pcrdattim= '2017-07-31-07.40.42.355440'
where exists 
(select
        b.CRDATTIM,
        b.RECORDCD,
        b.CRNODE,
        b.UNITCD,
        b.WRKTYPE
    from W03U999S b

    where a.PCRDATTIM = '2017-07-20-04.22.20.893340'
  and  a.CCRDATTIM = b.CRDATTIM 
  and a.CRECORDCD = b.RECORDCD 
  and a.CCRNODE = b.CRNODE
    and a.PRECORDCD = 'F'
    and a.PCRNODE = '01'
    and b.WRKTYPE = 'CALLER'
    and b.UNITCD='CS2XAA' and a.crecordcd='T');

答案 2 :(得分:-1)

这可能会有所帮助:

 <!DOCTYPE html>

      <html>
   
      <head>

    <link rel="stylesheet"
    type="text/css"
	href="css/looping.css">
	
    
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">                       
     </script> 
     <script src="looping.js"> </script>
     </head>
     <div id="mainwrapper"> 
     <body>
     <button  id="filler" onclick="myFunction()"> Fill in the box </button>
     <div id="imgbox"> </div>
     <div id="Obelix"> 
     <div id="Asterisk"> </div>  
     <div id="Getafix">  </div>
     </div>
     </div>
     </html>