create or replace procedure export_data_fromTempData(tableName varchar2, --表名
exportColumn varchar2, --列名
sequenceName varchar2, --序列名
columnsCount number) --列数
is
v_sql varchar2(4000);
v_strsql varchar2(4000);
v_col varchar2(100);
v_sequenceVal number;
v_values varchar2(4000) := '';
cursor curTemp is
select * from nk_excel_data;
begin
v_sql := 'insert into ' || tableName || ' (' || exportColumn ||
') values(';
for c in curTemp loop
select sequenceName.nextval into v_sequenceVal from dual; --这里怎么写,这样写肯定是错误的 sequenceName是个变量,不同的表是不一样的,不能写死 v_values := v_sequenceVal || ',';
begin
for d in 1 .. columnsCount loop
v_col := 'c.col' || d || ','; --这里怎么写呢,我是动态列名c.col+个整数
v_values := v_values || v_col;
end loop;
v_strsql := v_strsql || v_values;
v_values := '';
end;
v_strsql := substr(v_strsql, 1, length(v_strsql) - 1) || ')';
execute immediate v_strsql;
end loop;
commit;
end;
exportColumn varchar2, --列名
sequenceName varchar2, --序列名
columnsCount number) --列数
is
v_sql varchar2(4000);
v_strsql varchar2(4000);
v_col varchar2(100);
v_sequenceVal number;
v_values varchar2(4000) := '';
cursor curTemp is
select * from nk_excel_data;
begin
v_sql := 'insert into ' || tableName || ' (' || exportColumn ||
') values(';
for c in curTemp loop
select sequenceName.nextval into v_sequenceVal from dual; --这里怎么写,这样写肯定是错误的 sequenceName是个变量,不同的表是不一样的,不能写死 v_values := v_sequenceVal || ',';
begin
for d in 1 .. columnsCount loop
v_col := 'c.col' || d || ','; --这里怎么写呢,我是动态列名c.col+个整数
v_values := v_values || v_col;
end loop;
v_strsql := v_strsql || v_values;
v_values := '';
end;
v_strsql := substr(v_strsql, 1, length(v_strsql) - 1) || ')';
execute immediate v_strsql;
end loop;
commit;
end;
解决方案 »
- 触发器
- 日期格式转换问题
- 现在需要做一个oracle列表的查询
- 多台数据库的数据同步问题,急
- ORA-01467: sort key too long的的问题?在线等...........
- ORDER BY不能用在exists的句子里面吗??请各位指正一个SQL语句
- oracle8i中Oracle EnterPrise Manager中备份数据库时提示设置首选身份证明??
- oracle中照片字段用 blob,但用select *查询的时候,他说“列或属性类型无法通过 SQL*Plus 显示”?
- 定时执行任务
- oracle调整执行计划的问题
- 菜鸟求解在oracle中(2**32)的意思
- 执行效率问题
create or replace procedure export_data_fromTempData(tableName varchar2, --表名
exportColumn varchar2, --列名
sequenceName varchar2, --序列名
columnsCount number) --列数
is
v_sql varchar2(4000);
v_strsql varchar2(4000);
v_col varchar2(100);
v_sequenceVal number;
v_values varchar2(4000) := '';
cursor curTemp is
select * from nk_excel_data;
begin
v_sql := 'insert into ' || tableName || ' (' || exportColumn ||
') values(';
for c in curTemp loop
execute immediate 'select '|| sequenceName||'.nextval from dual' into v_sequenceVal; --这样写就可以了
v_values := v_sequenceVal || ',';
v_strsql := v_sql;
begin
for d in 1 .. columnsCount loop
v_col := 'c.col' || d || ','; --这里怎么解决呢,我是动态构造的col1,col2,col3...,有什么办法吗
v_values := v_values || v_col;
end loop;
v_strsql := v_strsql || v_values;
v_values := '';
end;
v_strsql := substr(v_strsql, 1, length(v_strsql) - 1) || ')';
--dbms_output.put_line (v_strsql);
execute immediate v_strsql;
end loop;
commit;
end;
这里只有一列exportColumn,
2.v_col := 'c.col' || d || ','; --这里怎么解决呢,我是动态构造的col1,col2,col3...
这么多都当成是value,就形成sql就不正确了【dbms_output.put_line (v_strsql);】
你这里面有这么多循环,都要干什么,尤其最里面那个?
1.不是一列啊,exportColumn 是逗号分隔的,很多列啊,传多小列就是多小列,如:taskid,taskname,taskparentid
我的问题就是在游标里,字段值怎样用变量的方式读取出来
c.col1这样当然可以
但我要这样 col1是一个变量,怎么实现,就这个问题
c.变量名,这种形式
能通过遍历该
user_tab_columns where table_name=upper('你的表名')
来动态抓你的列名来组合
【如果输入的表tablename和nk_excel_data表字段是相同的就很好实现】
-----------
用游标取值的时候你可以用fetch into放到变量里面,再取你要的列对应的变量
你也可以用
insert into 目标表(字段)
select 字段 from nk_excel_data;
的方式试试
create or replace procedure Getmax(tableName in varchar2, col in varchar2) is
v_deptno number; -- 规定好格式
v_dname varchar2(14);
v_loc varchar2(13);
v_sql varchar2(100);
cursor c1 is
select * from scott.dept;
begin
-- 判断字段col是哪几个字段,找出对应的变量
open c1;
loop
fetch c1
into v_deptno, v_dname, v_loc;
-- 拆分col 如:’deptno,loc' 拆分成 deptno和loc,并判断数据类型,在将对应的值加入动态sql
-- 以上面为例: || v_deptno || ',''' || v_loc || '''’ loc为varchar2所以必须加入''''
v_sql := 'insert into ' || tableName || '(' || col || ') values(' ||
v_deptno || ',''' || v_loc || ''')'; --仅以deptno,loc做示例,没有按上述方式处理
dbms_output.put_line(v_sql);
execute immediate v_sql;
commit;
exit when c1%notfound;
end loop;
close c1;
end Getmax;
我的列是传过来的,如传 t1,se,e2,对应的就是col1,col2,col3
传过来的是t1,se,对应的就是col1,col2
传过来的是dd...dd20,对应的就是col1...col20
每次传过来的列数是不一样的
(问题怎么在游标里用变量名来抓取值:用fetch into将所有字段的值放到变量里面)如果输入列是按照表结构的顺序中间有间隔,规定好变量格式按9楼的应该也可以
insert into table
(
column
)
select
(select * from user_tab_columns
where table_name=upper('你的表名')
and column_id <= 字段个数)
from nk_excel_data
where 条件;