脚本目的:将当前用户下的所有表的表名称和表对应的行数【count(*)】插入到TABLEROWCOUNTSTART表中遇到的困难:现在我已经把所有表的表名称插入到TABLENAME中,现在不知道如何将表名和这个表明对应的行数插入到TABLEROWCOUNTSTART中,例如系统有个order表,select count(*) from order是1000行,那么我就要执行
insert into TABLEROWCOUNTSTART(tableName,ncount)values('order',1000),我当前系统有900多个表小结:我应该在 脚本中的【--求助】位置如何写SQL来达到我上述目的declare  
 v_exists number; --判断表是否存在变量
 --name1 varchar(200);
 --sSQL varchar(500);
 begin
 --name1:='' ;
 --sSQL:='' ;
 --如果TABLENAME表存在就删除
select count(*) into v_exists from user_tables where table_name = 'TABLENAME';  
    if v_exists > 0 then  
   begin
   execute immediate 'drop table TABLENAME';
   v_exists:=0;
   end;
   end if;
 --如果 TABLEROWCOUNTSTART表存在就删除
select count(*) into v_exists from user_tables where table_name = 'TABLEROWCOUNTSTART';  
    if v_exists > 0 then  
   begin
   execute immediate 'drop table TABLEROWCOUNTSTART';
   v_exists:=0;
   end;
   end if; 
   --建立重新TABLENAME,TABLEROWCOUNTSTART两个表
   execute immediate 'create table TABLENAME(name varchar(2000))';
   execute immediate 'create table TABLEROWCOUNTSTART(TableName varchar(100), ncount int)';
   --将当前用户用户所有的表名称插入到TABLENAME表中
   insert into TABLENAME select TABLE_NAME from USER_TABLES; 
   begin
   declare 
       cursor Tcursor is 
       select * from TableName;
       Tcursor1 Tcursor%rowtype;
       begin
       open Tcursor;
       fetch Tcursor into Tcursor1;
       while Tcursor%found loop
       begin
       
        --求助
       fetch Tcursor into Tcursor1;
       end;
       end loop;
       end;
   end;
   end;
   

解决方案 »

  1.   

    每个用户都有对应的表空间名称的,--Oracle中用户表的表名和行数
    select table_name,num_rows from user_tables WHERE TABLESPACE_NAME = '你的当前用户使用的表空间名称(请一定使用大写)';
      

  2.   

    嗯,其实楼上是正解,不过我来完善下撒,呵呵
    完整的语句应该是:
    create table TABLEROWCOUNTSTART(tableName,ncount) as (select table_name,num_rows from user_tables where tablespace_name='SYSTEM');
    一般我们使用的默认表空间是SYSTEM,当然如果lz使用了其他的表空间只要改下名字就好,查看当前用户使用的表空间的语句是:
    select tablespace_name from user_tablespaces where user='当前用户的名称';