Oracle中执行动态SQL(execute immediate)

    为实现新增功能,在表test_tbl中新增序列。序列起始值要= max(id)+1 。尝试了两种两种写法(原理一样)都报错,后来同事提醒,加了一个execute immediate ''才创建成功。

    在Oracle的PL/SQL里:

execute immediate 代替了以前Oracle8i中dbms_sql package包。它解析并马上执行动态的sql语句或非运行时创建的pl/sql块。动态创建和执行sql语句性能超前,execute immediate的目标在于减小企业费用并获得较高的性能,较之以前它相当容易编码。尽管dbms_sql仍然可用,但是推荐使用execute immediate,因为它获的收益在包之上。 


--报错脚本一:

declare

  v_seqInit number;

begin

  select to_number(max(t.id) + 1) into v_seqInit from test_tbl t;

  create sequence test_tbl_seq increment by 1 start

    with v_seqInit nomaxvalue nocycle cache 10;

  commit;

end;

--报错脚本二:

create sequence test_tbl_seq increment by 1 start

  with(select to_number(max(t.id) + 1) from test_tbl t) nomaxvalue nocycle cache 10;

 

--正确脚本:

 

declare

  v_seqInit number;

begin

  select to_number(max(t.id) + 1) into v_seqInit from test_tbl t;

  execute immediate 'create sequence test_tbl_seq increment by 1 start with ' ||

                    v_seqInit;

  commit;

end;

参考资料:http://zhidao.baidu.com/question/317167678.html 

转载于:https://my.oschina.net/MiniBu/blog/304825

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值