INSERT语句中的子查询

时间:2015-10-09 02:45:04

标签: oracle stored-procedures plsql

我刚刚发现存储过程中的INSERT语句中不允许子查询。这是我的剧本:

begin
    execute immediate 'truncate table itcustadm.GL_DTPJ_TEST2';   
    insert into GL_DTPJ_TEST2
    (rule_no,
    posted_by_user_id,
    transaction_id,
    transaction_sr_no,
    dr_amount,
    cr_amount,
    tran_crncy_code,
    bkdt_tran_flg,
    bank_desc
    )
    select
    tq.rule_no,
    tq.posted_by_user_id,
    tq.transaction_id,
    tq.transaction_sr_no,
    tq.dr_amount,
    tq.cr_amount,
    tq.tran_crncy_code,
    tq.bkdt_tran_flg,
    (select ent.bank_desc from crmuser.end ent where ent.bank_id = gam.bank_id);

但是,由于Oracle不允许在SELECT语句底部的(从crmuser.end ent选择ent.bank_desc,其中ent.bank_id = gam.bank_id),所以最好的方法是什么?完成这个?

我实际上在INSERT语句之前有这个代码,但我不知道如何使用它:

get_bank_desc := '(select ent.bank_desc from crmuser.end ent ' ||
                'where ent.bank_id = gam.bank_id)';

4 个答案:

答案 0 :(得分:1)

我不确定你的目的是什么,但是下面的代码可能对你有用,你可以使用下面的查询示例将SubQuery输出插入表中,但要确保SubQuery的输出是单行o / p,以便您可以逃离"ORA-01427: single-row SubQuery returns more than one row" ERROR.

insert into test_ins1
values(1,(SELECT COL2 FROM TEST_INS WHERE COL1=1 ));

即使这样,你也可以在where where条件下使用rownum并获取单个值。

如有任何疑问,请通知我

答案 1 :(得分:0)

declare
     bank_desc_temp bank_desk_type; /* the type defined in crmuser.ent for bank_desc*/

begin

    select ent.bank_desc into bank_desc_temp from crmuser.end ent where ent.bank_id = gam.bank_id;

    execute immediate 'truncate table itcustadm.GL_DTPJ_TEST2';   
    insert into GL_DTPJ_TEST2
    (rule_no,
    posted_by_user_id,
    transaction_id,
    transaction_sr_no,
    dr_amount,
    cr_amount,
    tran_crncy_code,
    bkdt_tran_flg,
    bank_desc
    )
    select
    tq.rule_no,
    tq.posted_by_user_id,
    tq.transaction_id,
    tq.transaction_sr_no,
    tq.dr_amount,
    tq.cr_amount,
    tq.tran_crncy_code,
    tq.bkdt_tran_flg,
    bank_desc_temp;
end;

答案 2 :(得分:0)

当你说“不允许”时你的意思是什么?你收到错误了吗?

我问,因为子查询肯定允许在insert中作为select语句,只要你的语法正确(并且子查询最多返回一行),例如:

create table test_tab (col1 number, col2 varchar2(10));

begin
  insert into test_tab
  select 1,
         (select 'Yes' from dual d2 where d.dummy = d2.dummy)
  from   dual d;

  commit;
end;
/

select * from test_tab;

      COL1 COL2      
---------- ----------
         1 Yes          

您提供的代码存在一些语法问题 - from子句在哪里,以及tq和gam别名定义在哪里?

答案 3 :(得分:0)

您可以在insert语句中使用两种语法: (I)

  

INSERT INTO table_name(column1,column2 .... columnN)      VALUES(value1,value2 .... valueN);

(II)

  

INSERT INTO表(column1,column2,...)      SELECT expression1,expression2,...      来自source_table(s)      条件;

在您的示例中,您应该选择第二种方法:

  

插入GL_DTPJ_TEST2(rule_no,                             posted_by_user_id,                             TRANSACTION_ID,                             transaction_sr_no,                             dr_amount,                             cr_amount,                             tran_crncy_code,                             bkdt_tran_flg,                             bank_desc                             )   选择tq.rule_no,         tq.posted_by_user_id,         tq.transaction_id,         tq.transaction_sr_no,         tq.dr_amount,         tq.cr_amount,         tq.tran_crncy_code,         tq.bkdt_tran_flg,         ent.bank_desc   来自crmuser.gam   加入crmuser.end ent    在ent.bank_id = gam.bank_id   ;

基本上,如果要使用insert语句添加记录,则应首先使用完整的select语句。我将如何做到这一点: (1)

  

选择*   来自table1;

(2)

  

选择column1        ,列2        ,栏3   来自table1;

(3)

  

选择t1.column1        ,t1.column2        ,t1.column3        ,t2.column4        ,t2.column5   来自table1 t1   加入table2 t2    在t2.id = t1.id   ;

(4)

  

插入table3(col1                     ,COL2                     ,COL3                     ,COL4                     ,COL5)   选择t1.column1        ,t1.column2        ,t1.column3        ,t2.column4        ,t2.column5   来自table1 t1   加入table2 t2    在t2.id = t1.id   ;