在一些应用的时候,我们可能需要对表的列数进行查询,比如说,我们在做数据库复制的时候,需要对表的字段数有所限制,如何得到一个数据库所有表的列数哪?

 

使用游标处理

 

declare    xcursor   cursor    for

select    name    from    sysobjects   where    xtype= 'u'

declare    @name   varchar ( 100)

open    xcursor

fetch    xcursor   into    @name

create    table    #temptable   ( tablename varchar ( 300), rowcount1   int )

while    @@fetch_status = 0

begin

exec ( 'insert   into   #temptable   select   ''' + @name+ ''',count(*)   from   ' + @name)

fetch    xcursor   into    @name

end

close    xcursor

deallocate    xcursor

 

select    b. name, count ( a. name)    as    列数 from    syscolumns   a, sysobjects   b   , #temptable   c

where    b. xtype= 'u'    and    a. id= object_id ( b. name)    and    b. name= c. tablename

group    by    b. name

having count ( a. name)>= 246 -- 复制要求不能超过这个标准

drop table #temptable

 

 

posted on 2010-04-08 07:15  gaojier  阅读(389)  评论(0编辑  收藏  举报