如何将sql plus查询的rowcount返回到单独的文件

时间:2016-03-14 14:45:11

标签: sql sqlplus

我目前正在运行一个SQL PLUS脚本,它返回匹配的所有内容,并在反馈中将其写在extract.csv文件的末尾。但是,有更快的方法将反馈(选定的行)写入单独的文件。我目前只是围绕查询(第二个查询)包装了一个计数,但肯定必须有一个更快的方法,因为它存储在反馈中。只是无法在网上找到任何东西

set embedded on
set pagesize 0
set heading on
set linesize 256
set colsep ,   
set trimspool on 
set echo off 
set feedback on 
set termout off
SET UNDERLINE off 

spool C:\extract.csv

select * from stat1 st1
INNER JOIN numbers nmb 
on st1.numb LIKE '%' || nmb.numb || '%';

spool off 

spool C:\count.csv

select count(*) from (select * from stat1 st1
INNER JOIN numbers nmb 
on st1.numb LIKE '%' || nmb.numb || '%');

quit; 

3 个答案:

答案 0 :(得分:1)

请尝试使用SQL%ROWCOUNT

spool C:\extract.csv

select * from stat1 st1
INNER JOIN numbers nmb 
on st1.numb LIKE '%' || nmb.numb || '%';

dbms_output.put_line('Got: ' || SQL%ROWCOUNT);

spool off

您也可以创建新变量并使用SELECT INTO来表示此SQL%ROWCOUNT值。

答案 1 :(得分:0)

这是您应该为您的目的进行修改的一种方式:

<强> TEST.SQL:

set embedded on
set pagesize 0
set heading on
set linesize 256
set colsep ,   
set trimspool on 
set echo off 
set feedback on 
set termout off
SET UNDERLINE off 

variable v_cnt number;

COLUMN cnt NEW_VALUE v_cnt;

spool test_results.txt

select id, count(*) over () cnt
from   (select level id
        from   dual
        connect by level <= 100);

spool off;

execute :v_cnt := &v_cnt;

spool test_cnt.txt

print v_cnt;

spool off;

运行上述脚本后:

输出test_cnt.txt

 V_CNT
   100

输出test_results.txt

    ID,       CNT
     1,       100
     2,       100
     3,       100
     4,       100
     5,       100
     6,       100
     7,       100
     8,       100
     9,       100
    10,       100
    11,       100
    12,       100
    13,       100
    14,       100
    15,       100
    16,       100
    17,       100
    18,       100
    19,       100
    20,       100
    21,       100
    22,       100
    23,       100
    24,       100
    25,       100
    26,       100
    27,       100
    28,       100
    29,       100
    30,       100
    31,       100
    32,       100
    33,       100
    34,       100
    35,       100
    36,       100
    37,       100
    38,       100
    39,       100
    40,       100
    41,       100
    42,       100
    43,       100
    44,       100
    45,       100
    46,       100
    47,       100
    48,       100
    49,       100
    50,       100
    51,       100
    52,       100
    53,       100
    54,       100
    55,       100
    56,       100
    57,       100
    58,       100
    59,       100
    60,       100
    61,       100
    62,       100
    63,       100
    64,       100
    65,       100
    66,       100
    67,       100
    68,       100
    69,       100
    70,       100
    71,       100
    72,       100
    73,       100
    74,       100
    75,       100
    76,       100
    77,       100
    78,       100
    79,       100
    80,       100
    81,       100
    82,       100
    83,       100
    84,       100
    85,       100
    86,       100
    87,       100
    88,       100
    89,       100
    90,       100
    91,       100
    92,       100
    93,       100
    94,       100
    95,       100
    96,       100
    97,       100
    98,       100
    99,       100
   100,       100

100 rows selected.

答案 2 :(得分:0)

您可以使用Darkwing指示的dbms_output.put_line()命令,但必须以'exec'开头:

busboy