我现在PL/SQL中写了一条插入数据的语句,如下:
insert into test1(a,b,c,d) values((select x,y from test2),'12','34'),
我的本意是读取一张表中的两个字段如x,y作为值,外加12,34两个值共四个值插入test1表中,提示没有足够的值?
请教解决的方法。

解决方案 »

  1.   

    insert into test1(a,b,c,d)
    select x,y,'12','34' from test2
      

  2.   

    你这样的问题有:
    1.select x,y from test2;这样这个语句返回的值是一个数组。
    2.insert into test1(a,b,c,d) values(?,? ,'12','34');这个只是需要2个值。
    3.所以你应该像下面PL/SQL来进行操作。
    Thanks.--PL/SQL
    Declare
    TYPE cur_ is REF CURSOR;
    tmp_x varchar2(20);
    tmp_y varchar2(30);
    cur_ cur_type;
    begin
    Open cur_type for select x,y from test2;
    loop
    FETCH cur_type into tmp_x,tmp_y;
    EXIT WHEN cur_type%NOTFOUND;
    insert into test1(a,b,c,d) values(tmp_x,tmp_y,'12','34');
    commit;
    end loop;
    close cur_type;
    end
    /
      

  3.   

    insert into test1(a,b,c,d) 
    select x,y ,'12','34'
    from test2