[Switch] [SQLServer] Get SQL Server database user name, database name, all table names, all field names

  1. 1. Get all usernames:  
  2. Select name FROM Sysusers where status='2' and islogin='1'  
  3. islogin= '1'    : indicates the account  
  4. islogin= '0'    : indicates the role  
  5. status= '2'    : indicates the user account  
  6. status= '0'    : indicates the system account  
  7.   
  8. 2. Get all database names:  
  9. Select Name FROM Master..SysDatabases orDER BY Name  
  10.   
  11. 3. Get all table names:  
  12. Select Name FROM DatabaseName..SysObjects Where XType='U' orDER BY Name  
  13. XType= 'U'     : Indicates all user tables;  
  14. XType= 'S'     : Indicates all system tables;  
  15.   
  16. 4. Get all field names:  
  17. Select Name FROM SysColumns Where id=Object_Id('表名'')  
  18.   
  19. 5. Get all types of databases:  
  20. select name from systypes  
  21.   
  22. 6. Get the primary key field:  
  23. Select  name FROM SysColumns Where id=Object_Id('表名'and colid=(select top 1 keyno from sysindexkeys where id=Object_Id('表名'))  
  24.   
  25. 7. Get the basic information of table fields:  
  26.  code  
  27. Select  
  28.   field name=rtrim(b.name ) ,  
  29.   主键=CASE WHEN h.id IS NOT NULL  THEN 'PK' ELSE '' END,  
  30.   字段类型=type_name(b.xusertype)+CASE WHEN b.colstat&1=1 THEN '[ID(' + CONVERT(varchar, ident_seed(a.name))+','+CONVERT(varchar,ident_incr(a.name))+')]' ELSE '' END,  
  31.   length=b.length,   
  32.   允许空=CASE b.isnullable WHEN 0 THEN 'N' ELSE 'Y' END,   
  33.   Default = isnull (e.text,  '' ),  
  34.   field description = isnull (c.value,  '' )  
  35. FROM sysobjects a, syscolumns b  
  36. LEFT OUTER JOIN sysproperties c ON b.id = c.id AND b.colid = c.smallid  
  37. LEFT OUTER JOIN syscomments e ON b.cdefault = e.id  
  38. LEFT OUTER JOIN (Select g.id, g.colid FROM sysindexes f, sysindexkeys g Where (f.id=g.id)AND(f.indid=g.indid)AND(f.indid>0)AND(f.indid<255)AND(f.status&2048)<>0) h ON (b.id=h.id)AND(b.colid=h.colid)  
  39. Where  (a.id=b.id) AND (a.id=object_id( 'the table to be queried' ))   -- the table to be queried is changed to the name of the table you want to query  
  40. orDER BY b.colid  
  41.   
  42.  code  
  43. Select  
  44.   表名=case when a.colorder=1 then d.name else '' end,  
  45.   表说明=case when a.colorder=1 then isnull(f.value,''else '' end,  
  46.   字段序号=a.colorder,  
  47.   字段名=a.name,  
  48.   标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then '√' else '' end,  
  49.   主键=case when exists(Select 1 FROM sysobjects where xtype='PK' and name in (Select name FROM sysindexes Where indid in(Select indid FROM sysindexkeys Where id=a.id AND colid=a.colid))) then '√' else '' end,     
  50.   类型=b.name,  
  51.   字段长度=a.length,  
  52.   占用字节数=COLUMNPROPERTY(a.id,a.name,'PRECISION'),  
  53.   小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),  
  54.   允许空=case when a.isnullable=1 then '√'else '' end,  
  55.   默认值=isnull(e.text,''),  
  56.   字段说明=isnull(g.[value],'')  
  57. FROM syscolumns a  
  58. left join systypes b on a.xusertype=b.xusertype  
  59. inner join sysobjects d on (a.id=d.id)and(d.xtype='U')and(d.name<>'dtproperties')  
  60. left join syscomments e on a.cdefault=e.id  
  61. left join sysproperties g on (a.id=g.id)and(a.colid=g.smallid)  
  62. left join sysproperties f on (d.id=f.id)and(f.smallid=0)  
  63. --where d.name='要查询的表'         --如果只查询指定表,加上此条件  
  64. order by a.id,a.colorder 
  65. 系统存储过程
  66. 1、查看服务器角色相关信息 
    SP_HELPSRVROLE 
    SP_HELPSRVROLEMEMBER 服务器角色 
    SP_HELPSRVROLE 服务器角色 

    2、查看数据库角色相关信息 
    SP_HELPROLE 
    SP_HELPROLEMEMBER 数据库角色 
    SP_HELPROLE 数据库角色 

    3、查看用户相关信息 
    SP_HELPUSER 
    SP_HELPUSER 数据库用户名

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324890391&siteId=291194637