查询用户数据库的表、数据类型;通过T-Sql修改字段类型(SQL Server版))

  • 查询数据库中的所有数据库名:
    SELECT Name FROM Master..SysDatabases ORDER BY Name
    
  • 查询当前连接(某个)数据库中所有的表名:
    SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
    

其中XType='U' 的 U = User table,代表着使用获取当前用户的表。
注意,如果上面查询出来的结果掺杂着系统表,请加上条件:AND sysstat = '83' 也就是SELECT Name FROM SysObjects Where XType='U' AND sysstat = '83' ORDER BY Name
其他参数(如:S = System table)请参考微软官方说明https://docs.microsoft.com/en-us/sql/relational-databases/system-compatibility-views/sys-sysobjects-transact-sql?view=sql-server-2017

如果链接失效,请搜索关键字【 XType='U' 】

  • 查询【Blog】表,字段类型为【nvarchar】的所有字段
    select sys.objects.name as tableName,syscolumns.name as columnsName
    from syscolumns 
    LEFT OUTER JOIN sys.objects ON 
    syscolumns.id = sys.objects.object_id
    where  syscolumns.xtype = '231' and sys.objects.name = 'Blog'
    

其中,syscolumns.xtype 的值,通过 select TypeName = name,TypeID = system_type_id from sys.types order by TypeID 或者select distinct a.xtype,b.name from syscolumns a inner join systypes b on a.xtype=b.xtype order by a.xtype来查询得到,例如:nvarchar:231,varchar:167

  • 查询【UserInfo】表的【UserName】字段的“数据类型”和“长度”
    select column_name,data_type,CHARACTER_MAXIMUM_LENGTH from information_schema.columns 
    where table_name ='UserInfo' and COLUMN_NAME='UserName'
    
  • 修改【UserInfo】表的【UserName】字段的数据类型为【nvarchar(100)】
    alter table  UserInfo  alter column UserName nvarchar(100)
    
  • 查询当前表的架构名
    select schema_id, name, schema_name(schema_id) as SchemaName from sys.tables
    
  • 获取表的基本字段属性
      SELECT syscolumns.name,systypes.name,syscolumns.isnullable,
      syscolumns.length 
      FROM syscolumns, systypes 
      WHERE syscolumns.xusertype = systypes.xusertype 
      AND syscolumns.id = object_id('表名')
    
  • 查询数据库里所有的表属性
      SELECT   
       (case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空  
           a.colorder as 字段序号,  
           a.name as 字段名,  
           (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) as 标识,  
           (case when (SELECT count(*) FROM sysobjects--查询主键  
                           WHERE (name in  
                                   (SELECT name FROM sysindexes   
                                   WHERE (id = a.id)  AND (indid in  
                                           (SELECT indid FROM sysindexkeys  
                                             WHERE (id = a.id) AND (colid in  
                                               (SELECT colid FROM syscolumns  
                                               WHERE (id = a.id) AND (name = a.name))  
                               )))))   
               AND (xtype = 'PK'))>0 then '√' else '' end) as 主键,--查询主键END  
       b.name as 类型,  
       a.length as 占用字节数,  
       COLUMNPROPERTY(a.id,a.name,'PRECISION') as  长度,  
       isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,  
       (case when a.isnullable=1 then '√'else '' end) as 允许空,  
       isnull(e.text,'') as 默认值,  
       isnull(g.[value],'') AS 字段说明   
       FROM syscolumns a left join systypes b   
       on a.xtype=b.xusertype  
       inner join sysobjects d   
       on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'  
       left join syscomments e  
       on a.cdefault=e.id  
       left join sys.extended_properties g  
       on a.id=g.major_id AND a.colid = g.minor_id   
       order by a.id,a.colorder 
    

你可能感兴趣的:(查询用户数据库的表、数据类型;通过T-Sql修改字段类型(SQL Server版)))