2016-04-25 39 views
0

我想从'table1'基于created_date读取记录。如果table1中的created_date小于current_date,则需要更新table2中的布尔列。如何从表中读取并更新另一个表在Oracle数据库中使用存储过程?

例如:

Table 1 

    ID Col1 Col2 Created_Date 
    1 test test 25-Apr-2016 
    2 test test 23-Apr-2016 

    Table 2 

    ID Col11 Col12 Col_Boolean 
    2 test test false 

我需要更新列Col_Boolean在表2中,其中ID是从表1中的ID为CREATED_DATE小于当前日期。

+1

正是你尝试过什么/在哪里是你遇到了一个问题的代码? –

+0

@FrankSchmitt - 我还没有尝试过任何操作:-)我是新来的查询和存储过程 –

+0

表2中的ID = 2是否已经存在,您只需要更新布尔值?如果是的话,那么如何在ID = 1的时候没有行,布尔值设置为True?针对您的案例的解决方案可能是MERGE而不是UPDATE或INSERT,但这比您学习SQL时看起来更先进。顺便说一下,您知道Oracle数据库中没有布尔数据类型,对吧?您必须使用char或varchar2数据类型来模拟布尔值,对于Col_Boolean来说可能最好是char(1)类型,并且检查约束只允许为T或F. – mathguy

回答

1
update table_2 set col_boolean 'false' 
    where id in (select id from table_1 where created_date < sysdate); 
0
update table2 set col_boolean = 'false' 
where exists (select null 
      from table1 
      where table1.id = table2.id 
      and table1.created_date < sysdate); 
相关问题