试题:
表1:
ID MODEL VALUE
1 cpu 2
1 mem 4
2 cpu 4
3 mem 8
表2:
ID CPU MEM
1 ? ?
2 ? ?
3 ? ?
要求:用表一更新表二
如何使用列来更新行呢?Update语句是不可能了,应该使用游标向下循环来判断
表1中第二列的值,根据第二列的值不同执行不同的SQL语句。
create or replace procedure P_t12
as
var_col1 varchar2(200);
var_col2 varchar2(200);
var_col3 varchar2(200);
cursor cur is
select * from t1;
begin
for temp_record in cur loop
var_col1 := temp_record.col1;
var_col2 := temp_record.col2;
var_col3 := temp_record.col3;
if (temp_record.col2 = 'cpu') then
update t2 set cpu = var_col3 where t2.ID = var_col1;
elsif (temp_record.col2 = 'mem') then
update t2 set mem = var_col3 where t2.ID = var_col1;
end if;
commit;
end loop;
end;