免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: TOADLover
打印 上一主题 下一主题

看一段pl sql,很短,有什么语法错误? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2009-09-10 15:46 |只看该作者
手上没环境
insert into tbl1 values('4', 'abc4') where not exists (select * from tbl2 )
这样不行 就只能 count了

论坛徽章:
0
12 [报告]
发表于 2009-09-10 16:00 |只看该作者
有这样写的吗?
declare
v_char tbl2.col1id%type;
begin
select max(col1id) into v_char from tbl2 where col1id='4';
exception
when no_data_found then
insert into tbl2 values('4', 'def4');
end;

[ 本帖最后由 liangmatwo 于 2009-9-10 16:02 编辑 ]

论坛徽章:
0
13 [报告]
发表于 2009-09-11 10:01 |只看该作者
试试,不常写,不知道对不对

  1. begin
  2.            insert into tbl1 values('3', 'abc3');
  3.       begin     
  4.            select colid from tbl2
  5.              where colid = 4;
  6.       exception
  7.            when no_data_found then
  8.                      insert into tbl2 values('4', 'def4');
  9.       end;
  10. end;
复制代码

[ 本帖最后由 doni 于 2009-9-11 10:02 编辑 ]

论坛徽章:
0
14 [报告]
发表于 2009-09-11 10:04 |只看该作者
或者这样:

  1. insert into tbl2
  2. select '4', 'def4' from daul
  3.   where not exists (select 'x' from tbl2
  4.                                  where colid = '4')
复制代码

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-01-24 06:20:00
15 [报告]
发表于 2009-09-11 10:19 |只看该作者

回复 #13 doni 的帖子

pl/sql不能直接这么写的,select colid from tbl2 where colid = 4; 要写成select colid into some_variable from tbl2 where colid;

  1. begin
  2.            insert into tbl1 values('3', 'abc3');
  3.       declare
  4.            l_var varchar2(20);
  5.       begin
  6.            select colid into l_var from tbl2
  7.              where colid = 4;
  8.       exception
  9.            when no_data_found then
  10.                      insert into tbl2 values('4', 'def4');
  11.       end;
  12. end;
复制代码


我觉得和11楼说的那样,不用嵌套块的话也可以用count来做:

  1. declare
  2.         l_cnt number;
  3. begin
  4.            insert into tbl1 values('3', 'abc3');
  5.            select count(colid) into l_cnt from tbl2 where colid='4';
  6.            if l_cnt=0 then
  7.                    insert into tbl2 values('4','def4');
  8.            end if;
  9. end;
复制代码

[ 本帖最后由 Minsic 于 2009-9-11 10:22 编辑 ]

论坛徽章:
0
16 [报告]
发表于 2009-09-11 10:25 |只看该作者
原帖由 Minsic 于 2009-9-11 10:19 发表
pl/sql不能直接这么写的,select colid from tbl2 where colid = 4; 要写成select colid into some_variable from tbl2 where colid;

begin
           insert into tbl1 values('3', 'abc3');
      dec ...


平时不常写pl/sql,只是在forms里写写procedure & trigger,所以不太熟,见笑了

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-01-24 06:20:00
17 [报告]
发表于 2009-09-11 10:25 |只看该作者

回复 #14 doni 的帖子

这个好,支持

论坛徽章:
0
18 [报告]
发表于 2009-09-11 11:41 |只看该作者

回复 #1 TOADLover 的帖子

exists 不能这样用的 ! 只能用在关联里面是否存在这种关联,才能用exists;
你这个语句可以这样用
declare
  v_num number;
begin
           insert into tbl1 values('3', 'abc3');
           select count(1) into v_num from tbl2 where colid = 4 ;
           if v_num = 0 then
                     insert into tbl2 values('4', 'def4');
           end if;
end;

论坛徽章:
0
19 [报告]
发表于 2009-09-15 15:10 |只看该作者

回复 #18 wanlw61 的帖子

嗯,这个说的好,准确的解决方案,呵呵。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP