+ -
当前位置:首页 → 问答吧 → oracle 动态这行sql 语句 问题

oracle 动态这行sql 语句 问题

时间:2011-11-08

来源:互联网

sqlstr:='select d_c_id,
 (
  select sum(d_cost) from '||tablename||' aa where 
  (
  select count(*) from '||tablename||' where aa.d_c_id=d_c_id
  )>=1 and d_iscost=''0''
 )
 into cId,costmoney
 from '||tablename||' aa where 
 (
 select count(*) from '||tablename||' where aa.d_c_id=d_c_id
 )>=1 and d_iscost=''0''';
 
execute immediate sqlstr; 

这样执行会报错,但是执行生成的sql 语句就没有错 求解答,并求正确写法。

作者: lianqianxue   发布时间: 2011-11-08

这句SQL里有两个输出的参数,需要把他们赋值给变量
DECLARE
  v_Id NUMBER;
  v_costmoney NUMBER;
  tablename VARCHAR2 (30);
BEGIN
  sqlstr :=
  'select d_c_id,
  (
  select sum(d_cost) from '
  || tablename
  || ' aa where  
  (
  select count(*) from '
  || tablename
  || ' where aa.d_c_id=d_c_id
  )>=1 and d_iscost=''0''
  )
  into :1,:2
  from '
  || tablename
  || ' aa where  
 (
  select count(*) from '
  || tablename
  || ' where aa.d_c_id=d_c_id
  )>=1 and d_iscost=''0''';

  EXECUTE IMMEDIATE sqlstr USING OUT v_Id, OUT v_costmoney;
END;

作者: tx2730   发布时间: 2011-11-08

DECLARE
  v_Id NUMBER;
  v_costmoney NUMBER;
  tablename VARCHAR2 (30);
sqlstr varchar2(2000);
BEGIN
  tablename :='tab1';--your table name
  sqlstr :=
  'select d_c_id,
  (
  select sum(d_cost) from '
  || tablename
  || ' aa where  
  (
  select count(*) from '
  || tablename
  || ' where aa.d_c_id=d_c_id
  )>=1 and d_iscost=''0''
  )
  from '
  || tablename
  || ' aa where  
 (
  select count(*) from '
  || tablename
  || ' where aa.d_c_id=d_c_id
  )>=1 and d_iscost=''0''';

  EXECUTE IMMEDIATE sqlstr into v_Id, v_costmoney;
  DBMS_OUTPUT.put (v_Id || ',' || v_costmoney);
END;

作者: tx2730   发布时间: 2011-11-08