以下是其中一个选项。您只需为表中的每一行生成一个带有select语句的文件,然后执行它。
在这里,我有一个名为T1
的表,其中包含7条记录。该文件(让我们将其命名为tabletofiles.sql
)看起来是这样的:
set feedback off;
set termout off;
set heading off;
set verify off;
set sqlblanklines on;
# temp file with statements
spool /tmp/tmpfile.sql
# specify column names you want to fetch explicitly
select 'spool /tmp/file_' || to_char(rownum) || ';' || chr(10) ||
'select c1
from (select q.*
, rownum as rn
from &1 q)
where rn = ' || to_number(rownum) || ';' ||
chr(10) || 'spool off;'
from &1;
spool off;
@@tmpfile.sql
#clean up after ourselfs
!rm -I /tmp/tmpfile.sql
通表名作为参数
SQL> @/tmp/tabletofiles.sql t1
执行完成tmpfile.sql
后包含以下内容:
spool /tmp/file_1;
select c1
from (select q.*
, rownum as rn
from t1 q)
where rn = 1;
spool off;
spool /tmp/file_2;
select c1
from (select q.*
, rownum as rn
from t1 q)
where rn = 2;
spool off;
。 ..
每个结果select
语句被卷入不同的文件。
这里是我们的最后文件:
82 Jun 26 11:28 file_1.lst
82 Jun 26 11:28 file_2.lst
82 Jun 26 11:28 file_3.lst
82 Jun 26 11:28 file_4.lst
82 Jun 26 11:28 file_5.lst
82 Jun 26 11:28 file_6.lst
82 Jun 26 11:28 file_7.lst
的问题是具有标记'linux','unix'和'ksh',所以我发布了一个'awk'解决方案;如果解决方案必须在(PL /)SQL中完全执行,那么应该更新这个帖子来说明这一点(并且删除'linux' /'unix' /'ksh'标签) – markp