功能:实现动态添加表字段,并计算NUMBER类的字段的总和
有两张表 j,s
j表的字段是动态的,
s表中存储的是j表的字段信息可能描述不是很清楚,直接晒代码存储过程:for s in (select * from t_finance_detail_view) loop
pCount:=0;
for j in (select * from t_finance_option tfo where tfo.sch_id=sch_id and tfo.option_dtype=1) loop --pTempStr := 's.'||j.option_col;
--execute immediate pTempStr into pInt;
-- pInt:=s.(j.option_col);
--(j表的j.option_col是s的列名)
pMsg:=pMsg+','||j.option_name||':'||pInt;
if j.option_type=1 then
pCount:=pCount+pInt;
else
pCount:=pCount-pInt;
end if;
end loop;
end loop;
有两张表 j,s
j表的字段是动态的,
s表中存储的是j表的字段信息可能描述不是很清楚,直接晒代码存储过程:for s in (select * from t_finance_detail_view) loop
pCount:=0;
for j in (select * from t_finance_option tfo where tfo.sch_id=sch_id and tfo.option_dtype=1) loop --pTempStr := 's.'||j.option_col;
--execute immediate pTempStr into pInt;
-- pInt:=s.(j.option_col);
--(j表的j.option_col是s的列名)
pMsg:=pMsg+','||j.option_name||':'||pInt;
if j.option_type=1 then
pCount:=pCount+pInt;
else
pCount:=pCount-pInt;
end if;
end loop;
end loop;
就很说明我的目的,可惜它是错的,还请大侠们帮忙啊!
怎么实现这个问题就好了
for s in (select * from t_finance_detail_view) loop
pCount := 0;
for j in (select *
from t_finance_option tfo
where tfo.sch_id = sch_id
and tfo.option_dtype = 1) loop
--pInt:=s.(j.option_col);--取出s表中j.option_col列的值(怎么实现这句话)
pMsg := pMsg + ',' || j.option_name || ':' || pInt;
if j.option_type = 1 then
pCount := pCount + pInt;
else
pCount := pCount - pInt;
end if;
end loop;
end loop;
execute immediate 'select s.'||j.option_col||' from dual' into pInt;
pInt := s.xxx(这里是列名为'option_col'的值)
pMsg := pMsg + ',' || j.option_name || ':' || pInt;
if j.option_type = 1 then
pCount := pCount + pInt;
else
pCount := pCount - pInt;
end if;
end loop;end loop;
-- 你的问题可以用动态游标来解决-- 在变量声明区声明动态游标
TYPE TCUR IS REF CURSOR;
CUR TCUR;-- 使用动态SQL打开游标
OPEN CUR FOR 'SELECT '||j.opetion_col||' FROM t_finance_detail_view';
LOOP
-- 取出返回的结果
FETCH CUR INTO :放置结果的变量;
EXIT WHEN CUR%NOTFOUND;
END LOOP;
CLOSE CUR;
-- 你的问题可以通过动态游标来解决-- 首先在变量声明区声明动态游标变量
TYPE TCUR IS REF CURSOR;
CUR TCUR;-- 使用动态SQL打开游标
OPEN CUR FOR 'SELECT '||j.opetion_col||' FROM t_finance_detail_view';
LOOP
-- 获取结果
FETCH CUR INTO :放置结果的变量;
EXIT WHEN CUR%NOTFOUND;
END LOOP;
CLOSE CUR;