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 语句就没有错 求解答,并求正确写法。
(
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;
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;
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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28