一个存储过程实现如下要求:
列出当前数据库下,所有内容记录不为空(就是有记录)的用户表表的内容。
比如:
use database
create proc list_table_record
as
declare @count int
select @count=count(*) from 用户表--得到用户表总数
if @count>0
   begin
      while @count>0
        begin
           select * from 当前用户表 where (select count(*) from 该表)>0(就是保证该表中有记录),依次下去,取出所有用户表的记录内容
           set @count=@count-1
           下一个用户表
        end
    end类似上面的要求,请高手帮忙解答。不胜感谢。

解决方案 »

  1.   

    自已学学些就会了
    取当前用户表select @a = count(*) from sysobjects where xtype='U'
    循环
    while @i < @a
    begin
       
      set @i = @i + 1
    end
      

  2.   


    declare @cur cursor---游标
    declare @i int--数目
    declare @s varchar(20)--表名
    declare @sql varchar(100)--查询语句
    set @cur=cursor LOCAL SCROLL  for select name from sysobjects where xtype='U'
    open @cur
    set @i= @@CURSOR_ROWSfetch next from @cur into @s
    while @@FETCH_STATUS=0
    begin
    set @sql='select * from '+@s
    exec (@sql)
    fetch next from @cur into @s
    end
      

  3.   

    declare @cur cursor---游标
    declare @i int--数目
    declare @s varchar(20)--表名
    declare @sql varchar(100)--查询语句
    set @cur=cursor LOCAL SCROLL  for select name from sysobjects where xtype='U'
    open @cur--打开游标
    set @i= @@CURSOR_ROWS
    select @i as 表数目  
    fetch next from @cur into @s
    while @@FETCH_STATUS=0
    begin
    set @sql='select * from '+@s
    exec (@sql)
    fetch next from @cur into @s
    end
    close @cur
    DEALLOCATE @cur
      

  4.   

    To zjexe:
    如果我希望结果中含有每个表的名字就是把name字段读出来,应该怎么写?因为你的代码能够得出答案,但是我不知道是那个表的内容
      

  5.   

    还有一点没有实现,我希望不显示出没有记录的表,因为很多表没有记录,看起来很乱,所以我想去掉没有记录的表。有办法吗?我按照你的办法写了一下,但是不知道如何返回:
    creat proc re_count
    @s varchar (20)
    as
    declare @sql varchar(200)
    set @sql='select count(*) from'+@s
    exec(@sql)
    go
    就是先判断一下这个表是否有记录,如果有在显示,如何返回我写的这个存储过程的count值呢?
      

  6.   

    --如何将exec执行结果放入变量中? declare @num int, @sql nvarchar(4000) 
    set @sql='select @a=count(*) from tableName ' 
    exec sp_executesql @sql,N'@a int output',@num output 
    select @num 
      

  7.   

    declare @cur cursor---游标
    declare @i int--数目
    declare @s varchar(20)--表名
    declare @sql nvarchar(100)--查询语句
    declare @num int
    set @cur=cursor LOCAL SCROLL  for select name from sysobjects where xtype='U'
    open @cur--打开游标
    set @i= @@CURSOR_ROWS
    select @i as 表数目  
    fetch next from @cur into @s
    while @@FETCH_STATUS=0
    begin
    set @sql=N'select @a=count(*) from '+@s
    exec sp_executesql @sql,N'@a int output',@num output
    if  @num>0
    begin
    select @s 表名
    set @sql=N'select * from '+@s
    exec (@sql)
    end 
    fetch next from @cur into @s
    end
    close @cur
    DEALLOCATE @cur
      

  8.   

    ok,不过能否给讲讲
    1.set @sql=N'select @a=count(*) from '+@s
    大写的N'是什么意思?
    2.exec sp_executesql @sql,N'@a int output',@num output
    这句话是什么意思?
      

  9.   

    declare @sql nvarchar(100)--查询语句
    如上语句 因为前边声明的unicode 字符串 所以赋值的时候要加上N'sp_executesql是一个执行sql语句的存储过程
    语法
     
    sp_executesql [ @stmt = ] stmt

        {, [@params=] N'@parameter_name data_type [ OUT | OUTPUT ][,...n]' } 
         {, [ @param1 = ] 'value1' [ ,...n ] }
    ]
     其中@stmt 就是那个语句
    @stmt参数要用unicode 字符串