zoukankan      html  css  js  c++  java
  • 获取sql server数据库表的列数

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

     

    使用游标处理

     

    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

     

  • 相关阅读:
    0802作业1替换文本文件内容

    看病
    爬山
    作业1
    超市(未完成)
    图片复制
    替换
    文件
    英文字母和中文汉字在不同字符集编码下的字节数
  • 原文地址:https://www.cnblogs.com/Gaojier/p/2783636.html
Copyright © 2011-2022 走看看