手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle动态SQL的拼装要领

[日期:2012-11-13] 来源:Linux社区  作者:mikixiyou [字体:大 中 小]

使用using的好处,就是不用去转date类型为varchar类型,再转回去date类型这种繁琐的操作。


declare
incoming date:=sysdate-10;
outgoing int;
begin
execute immediate 'insert into t_object(a) select COUNT(*) FROM user_objects where created > :incoming' into outgoing using incoming ;
dbms_output.put_line(' count is: ' || outgoing);
end;

ORA-01007: 变量不在选择列表中
ORA-06512: 在 line 6


tom这样解释这个错误:Followup November 24, 2004 - 7am Central time zone:

you have to use DBMS_SQL when the number of outputs is not known until run time.

  1. declare
  2. v_cursor number; --定义游标
  3. v_string varchar2(2999);
  4. v_row number;
  5. begin
  6. v_string := 'insert into t_object(a) select COUNT(*) FROM user_objects where created > :incoming';--操作语句,其中:name是语句运行时才确定值的变量
  7. v_cursor:=dbms_sql.open_cursor;--打开处理游标
  8. dbms_sql.parse(v_cursor,v_string,dbms_sql.native);--解释语句
  9. dbms_sql.bind_variable(v_cursor,':incoming',sysdate-30); --给变量赋值
  10. v_row := dbms_sql.execute(v_cursor);--执行语句
  11. dbms_sql.close_cursor(v_cursor);--关闭游标
  12. --dbms_output.put_line(v_row);
  13. commit;
  14. exception
  15. when others then
  16. dbms_sql.close_cursor(v_cursor); --关闭游标
  17. rollback;
  18. end;
linux
相关资讯       Oracle SQL  Oracle SQL语句 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款