各位老大,我想建一张表,只有两个字段,视图名和视图的记录数,意思是,我想在数据库中建立一张表,然后将这个库中的所有视图的名字和每个视图的记录数统计出来,是用游标的方式吗,该怎么写语句呢,谢谢各位老大!

解决方案 »

  1.   

    create table v_table(Name sysname, row int)
    go
    declare test cursor for
    select 
    'insert v_table select '+quotename(Name,'''')+',row=count(1) from '+quotename(Name)
    from 
    sysobjects 
    where
    xtype='V'
    declare @Sql nvarchar(100)
    open test
    fetch next from test into @Sql
    while @@fetch_status=0
    begin
    exec(@Sql)
    fetch next from test into @Sql
    end
    close test
    deallocate testgo
    select * from v_table
      

  2.   

    use Northwind
    go
    declare @View_Name nvarchar(128)
    declare @RowCount int
    declare @sql nvarchar(4000)
    declare @View_Details table(ViewName nvarchar(128),ViewRowCount int)declare cur cursor for
        select name from sysobjects where xtype=N'V'
    open cur
    fetch next from cur into @View_Name
    while @@fetch_status=0
    begin
        set @sql=N'select @RowCount=count(*) from ['+@View_Name+N']'
        exec sp_executesql @sql,N'@RowCount int output',@RowCount output
        insert @View_Details values(@View_Name,@RowCount)
        fetch next from cur into @View_Name
    end
    close cur
    deallocate curselect * from @View_Details/*
    viewa.view 830
    Customer and Suppliers by City 120
    Alphabetical list of products 69
    Current Product List 69
    Orders Qry 830
    Products Above Average Price 25
    Products by Category 69
    Quarterly Orders 86
    Invoices 2155
    Order Details Extended 2155
    Order Subtotals 830
    Product Sales for 1997 77
    Category Sales for 1997 8
    Sales by Category 77
    Sales Totals by Amount 66
    Summary of Sales by Quarter 809
    Summary of Sales by Year 809
    syssegments 3
    sysconstraints 45*/