• Oracle 数据库执行批处理脚本


    1、执行批量赋值脚本

    /*** DML ***/
    declare
    
    v_sql varchar2(500) := '';
    
    v_condition VARCHAR2(200) := ' ';
    
    v_relationyear number :=0;
    
    v_relationmonth number :=0;
    
    v_relationweek number :=0;
    
    v_relationdays number :=0;
    
    v_relationhours number :=0;
    
    v_relationminute number :=0;
    
    v_relationseconds number :=0;
    
    v_relationworkorder number :=0;
    
    v_relationwarehose number :=0;
    
    v_relationworkgroup number :=0;
    
    begin
    
    v_sql := 'select
    
    t.relationyear,
    
    t.relationmonth,
    
    t.relationweek,
    
    t.relationdays,
    
    t.relationhours,
    
    t.relationminute,
    
    t.relationseconds,
    
    t.relationworkorder,
    
    t.relationwarehose,
    
    t.relationworkgroup
    
    from materialbatch t where 1=1' || v_condition;
    
    execute immediate v_sql
    
    into
    
    v_relationyear,
    
    v_relationmonth,
    
    v_relationweek,
    
    v_relationdays,
    
    v_relationhours,
    
    v_relationminute,
    
    v_relationseconds,
    
    v_relationworkorder,
    
    v_relationwarehose,
    
    v_relationworkgroup;
    
    DBMS_OUTPUT.put_line('v_relationyear--' || v_relationyear ||',---v_relationworkgroup---'||v_relationworkgroup);
    
    commit;
    
    end;

    2、执行删除表和创建表脚本

    /*** DDL ***/ 
    begin 
    EXECUTE IMMEDIATE 'drop table table_001'; 
    EXECUTE IMMEDIATE 'create table table_001(name varchar2(8),address varchar2(200))'; 
    end;

    3、执行插入脚本

    /*** DML ***/ 
    declare 
    v_1 varchar2(8); 
    v_2 varchar2(10); 
    str varchar2(50); 
    begin 
    v_1:='张三'; 
    v_2:='中国'; 
    str := 'INSERT INTO table_001(name ,address) VALUES (:1, :2)'; 
    EXECUTE IMMEDIATE str USING v_1, v_2; 
    commit; 
    end;

    4、返回结果集过程

    CREATE OR REPLACE package pkg_test as 
    /* 定义ref cursor类型 
    不加return类型,为弱类型,允许动态sql查询, 
    否则为强类型,无法使用动态sql查询; 
    */ 
    type myrctype is ref cursor; 
    
    --函数申明 
    function get(intID number) return myrctype; 
    end pkg_test; 
    / 
    
    CREATE OR REPLACE package body pkg_test as 
    --函数体 
    function get(intID number) return myrctype is 
    rc myrctype; --定义ref cursor变量 
    sqlstr varchar2(500); 
    begin 
    if intID=0 then 
    --静态测试,直接用select语句直接返回结果 
    open rc for select id,name,sex,address,postcode,birthday from 
    student; 
    else 
    --动态sql赋值,用:w_id来申明该变量从外部获得 
    sqlstr := 'select id,name,sex,address,postcode,birthday from student 
    where id=:w_id'; 
    --动态测试,用sqlstr字符串返回结果,用using关键词传递参数 
    open rc for sqlstr using intid; 
    end if; 
    
    return rc; 
    end get; 
    
    end pkg_test; 
    /

    5、返回单行结果

    declare  
        str varchar2(500);  
        c_1 varchar2(10);  
        r_1 test%rowtype;  
    begin  
        c_1:='张三';  
        str:='select * from test where name=:c WHERE ROWNUM=1';  
        execute immediate str into r_1 using c_1;  
        DBMS_OUTPUT.PUT_LINE(R_1.NAME||R_1.ADDRESS);  
    end ;
  • 相关阅读:
    并查集
    归并排序
    树的操作
    活动安排
    动态规划-股票交易
    网络流
    linux 展开
    linux 反引号、单引号、双引号
    linux 命令行快捷键
    判断一个点是否在三角形内部和边界上
  • 原文地址:https://www.cnblogs.com/JourneyOfFlower/p/14214746.html
Copyright © 2020-2023  润新知