`

plsql学习:dynamic SQL

 
阅读更多
#############################################
#
#About native dynamic SQL
#
# To improve the performance of dynamic SQL statements
# you can also use BULK EXECUTE IMMEDIATE,
# BULK FETCH, FORALL, and COLLECT INTO statements.
#
#  Structure is as follows:->
#  EXECUTE IMMEDIATE dynamic_SQL_string
#  [INTO defined_variable1, defined_variable2, ...]
#  [USING [IN | OUT | IN OUT] bind_argument1, bind_argument2,
#  ...][{RETURNING | RETURN} field1, field2, ... INTO bind_argument1,
#  bind_argument2, ...]
#
#############################################

-----------example A:EXECUTE IMMEDIATE statements------------------------
declare
   sql_stmt varchar2(100);
   plsql_block varchar2(300);
   v_zip varchar2(5) := '11106';
   v_total_student number;
   v_new_zip varchar2(5);
   v_student_id number := 151;
begin
  --Create table MY_STUDENT
  sql_stmt := 'create table my_student '||
              'as select * from student where zip = '||v_zip;
  execute immediate sql_stmt;
  
  --select total number of records from MY_STUDENT table 
  --and display results on the screen
  execute immediate 'select count(*) from my_student '
  into v_total_students;
  dbms_output.put_line('Students added:'||v_total_students);
  
  --select current date and display it on the screen
  plsql_block :=  'DECLARE '         ||
                  '   v_date DATE; ' ||
                  'BEGIN'            ||
                  '   select sysdate into v_date from dual;' ||
                  '   dbms_out.put_line(to_char(v_date,''DD-MON-YYY''));'||
                  'END;';
  execute immediate plsql_block;
  
  --update record in MY_STUDENT table
   /**
    *   update the MY_STUDENT table for a given student ID and return a new value of
    * zip code using the RETURNING statement. So, the EXECUTE IMMEDIATE command contains
    * both USING and RETURNING INTO options. The USING option allows you to pass a value of
    * student ID to the UPDATE statement at runtime, and the RETURNING INTO option allows you
    * to pass a new value of zip code from the UPDATE statement into your program.
   */
  sql_stmt : 'update my_student set zip = 11105 where student_id= :1' ||
             'returning zip into :2';
  execute immediate sql_stmt using v_student_id returning into v_new_zip;
  dbms_output.put_line('new zip code : '||v_new_zip);
END;
  
  
  --------------------------------------------------------------------------------

declare
  type student_cur_type is ref cursor;
  student_cur student_cur_type;
  
  v_zip varchar2(5) := '&sv_zip';
  v_first_name varchar(2);
  v_last_name varchar2(25);

begin
  open student_cur for
       'select first_name, last_name from student '||
       'where zip = :1'
       using v_zip;
       
  loop 
       fetch student_cur into v_first_name,v_last_name;
       exit when student_cur%notfound;
       
       dbms_output.put_line('First Name :'||v_first_name);
       dbms_output.put_line('Last Name :'||v_last_name);
  end loop;
  clost student_cur;
  
exception
  when others then
    if student_cur%isopen then
      close student_cur;
    end if;
    dbms_output.put_line('error: '|| substr(sqlerrm,1,200));
end;
  
  
  
分享到:
评论

相关推荐

    不用安装oracle客户端也可以用plsql连接远程oracle

    标题所述的“不用安装oracle客户端也可以用plsql连接远程oracle”是一种常见的技术实践,它允许开发者在不安装完整的Oracle客户端软件的情况下,通过PL/SQL Developer工具连接到远程Oracle数据库进行管理和开发工作...

    plsql.zip_PLSQL Developer_cursor

    在PL/SQL(Procedural Language/Structured Query Language)中,...通过阅读《PLSQL中显示Cursor、隐示Cursor、动态Ref_Cursor区别(有分支图解).pdf》这份资料,你可以更深入地了解这些概念,并结合实际例子进行学习。

    OracleProc的资料

    - Pro*C 提供了两种主要的编程模式:嵌入式SQL(Embedded SQL)和动态SQL(Dynamic SQL)。 2. **嵌入式SQL**: - 嵌入式SQL是将SQL语句直接插入到C程序的主体中。例如,声明SQL变量,执行SELECT、INSERT、UPDATE...

    alexandria-plsql-utils:Oracle PLSQL实用程序库

    亚历山大plsql实用程序 Oracle PL / SQL实用程序库 该库是PL / SQL各种实用程序包的集合,以及指向在其他位置托管和维护的有用库的链接。 使用PL / SQL生成PDF文件 PDF_BUILDER_PKG PDFGEN_PKG 使用PL / SQL生成...

    Oracle Database 12c PL-SQL programming

    Oracle Database 12c PL/SQL Programming explains how to retrieve and process data, write PL/SQL statements, execute effective queries, incorporate PHP and Java, and work with dynamic SQL. Code testing...

    ORACLE PLSQL性能调优诀窍与方法

    本书作者从PL/SQL入手,根据其特性,将其全书组织...第三部分(7-9章)描述了Dynamic SQL、缓冲机制及高级数据类型三类技术;第四部分(10-12章)结合大量的案例,介绍了性能调优的日常工作内容、版本控制的意义及作用等。

    11g_plsql_user_guide_and_reference.pdf

    Dynamic SQL has been improved with new features that make it easier to work with dynamic queries. Developers can now create more flexible and powerful queries on the fly. These enhancements include ...

    fos-plsql-dynamic-content

    FOS-PL / SQL动态内容 增强的PL / SQL动态内容区域,具有对刷新,延迟加载,微调框等的额外支持。 MIT许可下的免费插件 所有FOS插件都是根据MIT许可证发布的,这实际上意味着每个人都可以免费使用,无论是商业用途...

    MFC与数据库连接程序

    DECLARE_DYNAMIC(CMyRecordset) // 其他成员函数... }; CMyRecordset::CMyRecordset(CDatabase* pdb) : CRecordset(pdb) { m_strSQL.Format("SELECT * FROM YourTableName"); Open(CRecordset::forwardOnly, ...

    ORACLE资料

    EXECUTE IMMEDIATE dynamic_SQL_string [INTO defined_variable1, defined_variable2, ...] [USING [IN | OUT | INOUT] bind_argument1, bind_argument2, ...] [RETURNING INTO | RETURN bind_argument1, bind_...

    Install & Config APEX

    ##### 5.3 配置 DAD (Dynamic Authentication Domain) 为了让 `mod_plsql` 识别 APEX 请求,需要编辑 Http Server 的 Oracle Home 下的 `Apache\modplsql\conf` 目录中的 `dads.conf` 文件。以下是一个示例配置: `...

    Oracle-SQL-Scripts:我多年来编写的其他Oracle SQL脚本,涉及性能,统计信息和例行检查等

    Oracle SQL脚本漏洞: - PL/SQL compiler will report the very weird error of PLS-00103 if adding a comment after the call procedure clause in a trigger数据库设计演示: - Using a simple SQL Demo of DB ...

    oracle 12c new feature

    Oracle 12c引入了新的SQL执行引擎优化机制,包括动态采样(Dynamic Sampling)、自适应执行计划(Adaptive Execution Plans)和并行查询(Parallel Query)等。这些优化措施共同提高了查询的响应时间和整体系统性能。 ###...

Global site tag (gtag.js) - Google Analytics