如何从oracle中的现有记录表中查找记录?

时间:2014-01-05 21:40:02

标签: oracle select plsql row

这是一个包含大量列的表

create table test (
id    number,
col_1 varchar2(50),
col_2 varchar2(50),
.
.
col_n varchar2(50)
);

并且表中填充了一些示例数据

insert into test values(1,'a1','a2',...,'an');
insert into test values(2,'a1','a3',...,'am');
insert into test values(3,'a4','a2',...,'ax');

现在我需要复制一行(例如,id = 1的行),如果结果与另一行不相似(不考虑id),则只更改一列的值。 这样的事情:

declare
 r test%rowtype;
 var1 number;
begin
 insert into r from test where id = 1;
 r.col_2='a3';
 select count (*) into var1 from test where 'the row without id' = r;
 if (var1 = 0) then
  insert into test values r;
 end if;
end;

但我不知道如何在oracle中编写select部分。考虑表测试有很多列,所以你不能写where子句中的所有列。

1 个答案:

答案 0 :(得分:2)

有点难以理解你需要什么。我会在这里拍摄。让我知道这是否正常......

为了方便起见,您可以在桌面上创建一个独特的索引吗?

    create unique index test_uidx on test (col_1, col_2, ... col_n); 

然后让oracle完成工作:

    declare
      r     test%rowtype;
      var1  number;
    begin
      select * into r from test where id=1;  --- get the row
      r.col_1  := 'some new value';          --- change 1 value

      begin
         insert into test values r;          --- insert the row
      exception
        when dup_val_on_index then           --- row was a dup
          null;                              --- and did not insert
      end;
   end;