#!/usr/bin/bash
obj_type="$1"中间省略....set trimspool on
spool "table_count.sql"
select 'select * from (' from dual;
select replace(' select '||'''' || 'REP_TBL_NAME' ||''''  ||' as TABLE_NAME,count(*) as CNT from REP_TBL_NAME union','REP_TBL_NAME',object_name) from user_objects where object_type='obj_type';
select 'select '|| '''' ||'' || '''' || ',-1 from dual ) order by 1;' from dual;
spool off
虽然知道这个脚本是计算表的记录数,但是对里面的句子有点不理解。1.用''''有什么用?直接select replace('select '||'REP_TBL_NAME' ||' as TABLE_NAME,count(*) as CNT from REP_TBL_NAME union','REP_TBL_NAME',object_name) from user_objects where object_type='obj_type';2.select 'select '|| '''' ||'' || '''' || ',-1 from dual ) order by 1;' from dual;
 -1是什么意思?家里环境是window7,公司环境是linux,想在家做一下可是不知道怎么做。

解决方案 »

  1.   

    1.''''此处第一个转义,第二个转义后的'保留
    2.-1没什么意思,是你的计算数量的列,因你的脚本有两列,第一列是对象名称,每二列是数量
    --你的脚本执行后就像这样子:
    select * from ( 
     select 'EMP' as TABLE_NAME,count(*) as CNT from EMP union      
     select 'DEPT' as TABLE_NAME,count(*) as CNT from DEPT union      
    select '',-1 from dual ) order by 1; 
      

  2.   

    1、转义字符,为了结果里出现',需要另外的单引号进行转义
    2、-1就是凑数的,0可以,-2也可以,不过是为了凑齐union中的列数。
      

  3.   

    那不是变成:
    'select '||''||'REP_TBL_NAME'||''||' as TABLE_NAME,count(*) as CNT from REP_TBL_NAME union','REP_TBL_NAME',object_name再写明白一点就是:
    'select ''REP_TBL_NAME'' as TABLE_NAME,count(*) as CNT from REP_TBL_NAME union'...
    还要再来一次转义又变成:
    'select 'REP_TBL_NAME' as TABLE_NAME,count(*) as CNT from REP_TBL_NAME union'...不知道我这样理解对吗?
    转这么多次,脑子转不过来啊。