Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DB2更新sql中的内部联接_Sql_Db2 - Fatal编程技术网

DB2更新sql中的内部联接

DB2更新sql中的内部联接,sql,db2,Sql,Db2,有没有办法在DB2的update语句中使用连接 谷歌真的让我失望了 这大概就是我想要达到的目标(除了工作之外) Cheers加入update语句是非标准的,并非所有供应商都支持。您尝试执行的操作可以通过子选择来完成: update file1 set firstfield = (select 'stuff' concat something from file2 where substr(file1.field1, 10, 20) = substr(file2.xxx,1,10) ) w

有没有办法在DB2的update语句中使用连接

谷歌真的让我失望了

这大概就是我想要达到的目标(除了工作之外)


Cheers

加入
update
语句是非标准的,并非所有供应商都支持。您尝试执行的操作可以通过子选择来完成:

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%'

在标准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%' 

这种类型的东西在Oracle或SQL Server上可以使用,而且(尽管我手头没有DB/2实例要测试)几乎肯定可以在DB/2上使用。

关于DB2 LUW 9.7的参考文档给出了以下示例:

   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,这意味着任何有效的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的映射。如果有多个匹配的行,它将不起作用。

您不需要说明目标平台。不过,将表称为文件使我相信您并没有在Linux、UNIX或Windows(LUW)上运行DB2

但是,如果您使用的是DB2 LUW,请参见以下语句:

对于您的示例语句,可以这样写:

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)。我不确定这是否是故意的,但这可能会影响你的比较。

这里有一个很好的例子,说明我刚刚开始工作:

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
)
更新答案:

如果要多列,可以通过以下方式实现:

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%'

来源:

试试这个,然后告诉我结果:

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) 

仅更新与条件匹配的行,并避免更新其他行中的空值:

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中工作,您可以这样问

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%'
如果join给出多个结果,您可以像这样强制更新

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)
) 

感谢ConcernedOfTunbridge-不幸的是,这在DB2中不起作用,我收到一个错误,说“列限定符或表B未定义”您是否尝试了| |而不是+,并且您希望键入内部联接而不是“联接”-我认为substr函数应该是“10,10”而不是“10,20”-请参阅标准SQL不支持UPDATE语句中的联接。。。这就是SQL Server语法;MySQL支持UPDATE语句中的连接,但不支持这种语法。不能更新包含连接的fullselect,就像不能更新包含连接的视图一样(没有INSTEAD OF触发器)。此语句将返回SQL01050N。我不知道您是否注意到,但OP最后一次看到她是在2011年1月24日13:03。他/她不太可能回来。@Linger是真的,但多年后搜索者仍能找到答案。如果答案是有效的,那么它总是相关的。关于“file”的用法的有趣的发现,在使用IBMi的开发人员中,“file”通常与“table”互换使用。在这个操作系统中,DB2表是一个外部描述的物理数据库文件。但是,可执行程序对象不是文件。谢谢!你解决了我的问题。(DB2 v11.1.4.4)更新[schema].TableA集合Column1='Foo',Column2='Bar'存在的位置(从[schema]中选择1.TableB其中[schema].TableB.CommonColumn=[schema].TableA.CommonColumn和[schema].TableB.key='condition');我以前没有使用过这种语法,但奇怪的是,它比连接好得多。
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)
)