我要删除表中某个字段值不在数组中的元素,请问sql语句怎么写,谢谢
DNS 是一个字符串数组
DNS.EXTEND;
DNS(1):='yy';
v_SQL:='DELETE FROM USER_INFO WHERE USER_DN NOT IN :1';
EXECUTE IMMEDIATE v_SQL USING DNS;
DNS 是一个字符串数组
DNS.EXTEND;
DNS(1):='yy';
v_SQL:='DELETE FROM USER_INFO WHERE USER_DN NOT IN :1';
EXECUTE IMMEDIATE v_SQL USING DNS;
type array_type is varray(20) of source_table.col1%type;
c array_type := array_type(null);--c中为目标表某个字段数据集合
b array_type := array_type(null);--b中为要检索的数组,b中可以是给定的一个集合,也可以是某张表中的某个字段值的集合
cn number;
nm number;
k number := 0;
begin
select count(*) into cn from source_table;
c.extend(cn-1);
dbms_output.put_line('************c.count='||c.count);
select col1 bulk collect into c from source_table order by col1;--col1为要删除的某个字段
--当b为某个表某个字段的集合时
select count(*) into cn from recrd_table;
b.extend(cn-1);
dbms_output.put_line('************b.count='||b.count);
select col2 bulk collect into b from recrd_table order by col2;--col2为某表中某字段
dbms_output.put_line('******------------------******');
/*
当b为给定的集合时,直接在声明部分把给定的值列出即可,就不需要"当b为某个表某个字段的集合时"这部分处理了
*/
for i in 1..c.count loop
k := 0;
for j in 1..b.count loop
if c(i) = b(j) then
dbms_output.put_line('****find the record,neednot delete********');
k := 1;
exit;
end if;
end loop;
if k = 0 then
dbms_output.put_line('---delete the record where col1 is equals to '||c(i));
delete from source_table where col1 = c(i);
end if;
end loop;
end ;