oracle PL/SQL 中變數綁定用法

來源:互聯網
上載者:User

從Oracle的共用池的設計、和Oracle推薦的 PL/SQL 寫法中,可以看出,變數綁定對效能有比較大的影響,那麼,如何在PL/SQL 中使用變數綁定呢?

首先看看不使用變數綁定的用法:

declare

    cursor cur_temp(id number) is
            select * from table_a where a=id;

     c_temp cur_temp%rowtype;

beign

  open cur_temp(1);

  loop

    fetch cur_temp into c_temp;

   exit when cur_temp%notfound;

  insert into b values (c_temp.a);

    end loop;

close cur_temp;

commit;

end;

上面是沒有使用變數綁定的用法,包括遊標和動作陳述式。

再看下面使用變數綁定的用法:

先要

type cursorType is ref cursor;

然後:

declare

    cur_temp cursortype;

   c_temp table_a%rowtype;

begin

  open cur_temp for 'select * from table_a where a=:1' using 91;

   loop

       fetch cur_temp into c_temp;

       exit when cur_temp%notfound;

      execute immediate 'insert into b values (:1)' using c_temp.a;

  end loop;

close cur_temp;

commit;

end;

上面是在PL/SQL 塊中的寫法,這個寫法也同樣適用於預存程序,觸發器,函數,包等可以用PL/SQL 的地方。

對於需要用 INTO 的地方,可以如下使用:

  i number(6);

execute immediate 'select count(*) from table_a where a =:1' into i using 89;

執行後,取出的值存放在了變數 i 中。

在PL/SQL 中,變數綁定的常見用法基本如上所示,建議全部使用變數綁定。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.