• Oracle 绑定变量的用法


    1.让Oracle自己绑定变量
    set serverout on;
    set timing on;
    declare
    l_sql varchar2(2000);
    l_count number;
    l_param1 varchar2(100);
    l_param2 varchar2(100);
    begin
    l_param1:=’a';
    l_param2:=’b';
    select count(*) into l_count from table1 where col_1=l_param1 and col_2=l_param2;
    dbms_output.put_line(l_count);
    end;
    /
    在上面的情况,Oracle会自己绑定变量,即,如果参数保存在一个数组中,select语句放在一个循环中,
    select 语句只会编译一次。
    像这样
    for i in 1..3
    loop
    select count(*) into l_count from table1 where col_1=l_param1 and col_2=l_param2 and col_3=i;
    dbms_output.put_line(l_count);
    end loop

    2.不绑定变量
    set serverout on;
    set timing on;
    declare
    l_sql varchar2(2000);
    l_count number;
    l_param1 varchar2(100);
    l_param2 varchar2(100);
    begin
    l_param1:=’a';
    l_param2:=’b';
    l_sql:=’select count(*) into :x from table1 where col_1=’||l_param1||’ and col_2=’||l_param2;
    Execute Immediate l_sql into l_count;
    dbms_output.put_line(l_count);
    end;
    /
    3.动态绑定变量
    set serverout on;
    set timing on;
    declare
    l_sql varchar2(2000);
    l_count number;
    l_param1 varchar2(100);
    l_param2 varchar2(100);
    begin
    l_param1:=’a';
    l_param2:=’b';
    l_sql:=’select count(*) into :x from table1 where col_1=:y and col_2=:z ’;
    Execute Immediate l_sql into l_count using l_param1,l_param2;
    dbms_output.put_line(l_count);
    end;
    /

    :x,:y,:z相当于占位符,即
    1.用:p1,:p2,:p3是一样的。
    2.用:x,:x,:x也是一样的
    需要的绑定变量按顺序排在执行语句后面就可以了,into的除外。
    不过还是用p1,p2好些,至少可以看出绑定了多少个变量。
  • 相关阅读:
    从七牛云迁移图片到github
    Ajax 结构及使用
    JQuery 简单的文字超出部分隐藏下拉显示
    JQuery 动画及一些小知识点
    Jqurey DOM 操作详解
    Jquery DOM 操作列表
    文件上传以及JS链式结构
    JQuery选择器
    JQuery 基本知识
    LinQ to sql 各种数据库查询方法
  • 原文地址:https://www.cnblogs.com/wuyisky/p/oracle_bind.html
Copyright © 2020-2023  润新知