ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Native Dynamic SQL and Dynamic PL/SQL

Native Dynamic SQL and Dynamic PL/SQL

原创 Linux操作系统 作者:susie0409 时间:2010-03-02 22:07:06 0 删除 编辑
DBMS_SQL vs. Native Dynamic SQL

Add a Note HereAlthough you can use DBMS_SQL to perform. dynamic SQL and dynamic PL/SQL, native dynamic SQL is better suited to performing DML, DDL, PL/SQL, and transaction and session control statements for the following reasons:

  • Add a Note HereNative dynamic SQL enables you to place dynamic SQL statements and dynamic PL/SQL blocks directly into PL/SQL code, without the use of an API. This is similar to static SQL, and it's much easier in native dynamic SQL than in DBMS_SQL in terms of coding.

  • Add a Note HereThe "native" in native dynamic SQL refers to the built-in support for dynamic SQL inside the PL/SQL interpreter. Native dynamic SQL is also integrated with SQL, so its performance is on par with static SQL's. There is no API involved, thus the high costs involved in procedure call and data copying when using DBMS_SQL are minimized.

  • Add a Note HereNative dynamic SQL supports the full range of object extensibility features, such as user-defined types and objects including collections and REFs, within PL/SQL. These features aren't supported by the DBMS_SQL package.

  • Add a Note HereNative dynamic SQL, like static SQL, supports fetching into records of type %ROWTYPE or as defined by the TYPE … IS RECORD … statement.

Add a Note HereSo what use is DBMS_SQL then?

  • Add a Note HereDBMS_SQL is well suited to performing dynamic SQL and PL/SQL from client-side applications such as Oracle Developer.

  • Add a Note HereThe maximum length of the dynamic SQL statement is limited to 32KB. Statements larger than this can be handled only by the DBMS_SQL package.

  • Add a Note HereDBMS_SQL is good when the same SQL statement is executed multiple times in the same session as it reuses the parsed SQL statement. Native dynamic SQL prepares the SQL statement each time it is processed.

Add a Note HereHere's an example of the code involved in creating a table whose name is dynamically constructed using native dynamic SQL and DBMS_SQL:

Add a Note Here
/* Native Dynamic SQL */
DECLARE
  dyn_tab_name VARCHAR2(30) := 'temp';
  dyn_string VARCHAR2(150);
BEGIN
  dyn_string := 'create table '||dyn_tab_name||' ( col1 NUMBER NOT NULL) ';
  EXECUTE IMMEDIATE dyn_string;
END;
/

Add a Note HereYou can verify the output of this PL/SQL block as follows:

Add a Note HereUsing native dynamic SQL involves just two steps:

  1. Add a Note HereConstruct the dynamic SQL statement in the form. of string.

  2. Add a Note HereExecute the SQL statement.

Add a Note Here
drop table temp;
-- DBMS_SQL
DECLARE
  dyn_tab_name VARCHAR2(30) := 'temp';
  dyn_string VARCHAR2(150);
  c INTEGER;
  ret_code INTEGER;
BEGIN
  dyn_string := 'create table '||dyn_tab_name||
                                         ' ( col1 NUMBER NOT NULL) ';
  c := DBMS_SQL.OPEN_CURSOR;
  DBMS_SQL.PARSE(c, dyn_string, DBMS_SQL.V7);
  ret_code := DBMS_SQL.EXECUTE(c);
   DBMS_SQL.CLOSE_CURSOR(c);
END;
/

Add a Note HereUsing DBMS_SQL involves five steps:

  1. Add a Note HereConstruct the dynamic SQL statement in the form. of string.

  2. Add a Note HereDeclare a cursor handle and open a cursor for the handle.

  3. Add a Note HereParse the dynamically constructed SQL statement and associate the SQL statement with the cursor handle.

  4. Add a Note HereExecute the SQL statement.

  5. Add a Note HereClose the cursor handle.

Add a Note HereAs you can see, it's much easier to code with native dynamic SQL than with DBMS_SQL. Also, depending on the type of SQL statement, DBMS_SQL involves more steps than native dynamic SQL. For example, to use bind variables, an extra step of explicitly binding the variables to the values is required in DBMS_SQL.

Add a Note HereAlso, in the case of SELECT statements with DBMS_SQL, two extra steps of defining the columns in the SELECT and retrieving values from the dynamic SQL into local PL/SQL variables are required in addition to the execute and FETCH steps.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15225049/viewspace-628431/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-09-30

  • 博文量
    115
  • 访问量
    305727