日期:2014-05-17  浏览次数:21073 次

在存储过程里“execute immediate v_sql”与直接使用sql有什“execute immediate v_sql”与直接使用sql有什么区别?
execute immediate v_sql的例子:
  begin
  v_sql := 'select channel_id from imp_sh_cms_channel where wg_code =' ||
  in_wg_id;
  execute immediate v_sql
  into v_contentid;
  exception
  when no_data_found then
  out_result := 1; -- can't find data in imp_sh_cms_channel
  rollback;
  return;
  end;


直接使用sql的例子:
  begin
  select channel_id into v_contentid from imp_sh_cms_channel where wg_code=in_wg_id;
  exception
  when no_data_found then
  out_result := 1; -- can't find data in imp_sh_cms_channel
  rollback;
  return;
  end;

------解决方案--------------------
就单纯的你这个例子来说
两者返回的结果是一样的,想你这样情况就没有必要用动态SQL
------解决方案--------------------
此例没必要用动态SQL。


动态SQL是指SQL文不确定时,不同条件下,拼接出不同SQL文。


------解决方案--------------------
第一种动态执行,不使用绑定变量的;第二种则使用绑定变量

------解决方案--------------------
你只传入一个值的话,效率上基本没什么区别
另外用动态SQL的另一种情况就是在programme中执行DDL语句
------解决方案--------------------
执行的SQL:

--每一个值不一样,SQL语句也不一样。也有可能导致执行计划不一样
select channel_id from imp_sh_cms_channel where wg_code = '123';
select channel_id from imp_sh_cms_channel where wg_code = '456';

--这是使用绑定变量
select channel_id from imp_sh_cms_channel where wg_code = :code;

google一下:绑定变量

http://www.google.com.hk/search?hl=zh-CN&source=hp&q=%E7%BB%91%E5%AE%9A%E5%8F%98%E9%87%8F&meta=&aq=f&aqi=&aql=&oq=&gs_rfai=