oracle 中的插入语句要条件判断怎么写?

2024-11-21 22:32:29
推荐回答(3个)
回答1:

insert into table1 values(a1,a2,aid in(select aid from p where p.name=''),cid in (select sid from u where u.name='aa'))
应该要把 aid in ,cid in去掉吧?

最简单的方式,把后面两个字段设置为非空,让oracle报错去。。呵呵

回答2:

既然你的数据库中该字段可以为空,那为什么非要不为空呢?
declare
v_aid character(10);
v_cid character(10);
select adi from p into v_aid where p.name='';
select cid from u into v_cid where u.name='aa';
if (v_aid is not null ) and (v_cid is not null)
then insert into table 1 values(a1,a2,v_aid,v_cid);
else
then 输出错误信息(好长时间没有写过了 忘了)
end if;

回答3:

要写成判断
if (select count(aid) from p where p.name='')<>0 and (select count(sid) from u where u.name='aa')<>0
begin
insert into table1 values(a1,a2,aid,cid)
end
else
begin
raiserror 50001 ' 无法插入数据! '
rollback transaction
end