当前位置:Gxlcms > mysql > 对Sqlserver的高级操作

对Sqlserver的高级操作

时间:2021-07-01 10:21:17 帮助过:18人阅读

列出某数据库下的所有表 SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' 或者 SELECT OBJECT_NAME (id) FROM sysobjects WHERE xtype = 'U' AND OBJECTPROPERTY (id, 'IsMSShipped') = 0 或者 使用SQLServer的存储过程s

列出某数据库下的所有表

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

或者

SELECT OBJECT_NAME (id) FROM sysobjects WHERE xtype = 'U' AND OBJECTPROPERTY (id, 'IsMSShipped') = 0

或者

使用SQLServer的存储过程“sp_tables”

ADOX是ADO Extensions for DDL and Security,是微软对ADO技术的扩展,使用它我们可以操作数据库的结构。它是一个COM组件,估计以后在ADO.NET中会增加ADOX的一些功能。如果大家需要ADOX的一些资料,我可以提供。下面的一个例子就是使用ADOX来获得当前数据库的所有表。

摘抄

1.获取所有数据库名:
SELECT Name from Master..SysDatabases ORDER BY Name

2.获取所有表名:
SELECT Name from DatabaseName..SysObjects Where XType='U' ORDER BY Name
XType='U':表示所有用户表;
XType='S':表示所有系统表;

3.获取所有字段名:
SELECT Name from SysColumns WHERE id=Object_Id('TableName')

access :


如果表存在,如何得到这个表?
SELECT * from MSysObjects where name='ExamResultTime ' and type=1 and flags=0


//得到该数据库中所有的表名
SELECT name from MSysObjects where type=1 and flags=0

    1. 1、获取所有用户名:
    2. Select name FROM Sysusers where status='2' and islogin='1'
    3. islogin='1' :表示帐户
    4. islogin='0' :表示角色
    5. status='2' :表示用户帐户
    6. status='0' :表示糸统帐户
    7. 2、获取所有数据库名:
    8. Select Name FROM Master..SysDatabases orDER BY Name
    9. 3、获取所有表名:
    10. Select Name FROM DatabaseName..SysObjects Where XType='U' orDER BY Name
    11. XType='U' :表示所有用户表;
    12. XType='S' :表示所有系统表;
    13. 4、获取所有字段名:
    14. Select Name FROM SysColumns Where id=Object_Id('表名'')
    15. 5、获取数据库所有类型:
    16. select name from systypes
    17. 6、获取主键字段:
    18. Select name FROM SysColumns Where id=Object_Id('表名') and colid=(select top 1 keyno from sysindexkeys where id=Object_Id('表名'))
    19. 7、获取表字段的基本信息:
    20. 程序代码
    21. Select
    22. 字段名=rtrim(b.name),
    23. 主键=CASE WHEN h.id IS NOT NULL THEN 'PK' ELSE '' END,
    24. 字段类型=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,
    25. 长度=b.length,
    26. 允许空=CASE b.isnullable WHEN 0 THEN 'N' ELSE 'Y' END,
    27. 默认值=isnull(e.text, ''),
    28. 字段说明=isnull(c.value, '')
    29. FROM sysobjects a, syscolumns b
    30. LEFT OUTER JOIN sysproperties c ON b.id = c.id AND b.colid = c.smallid
    31. LEFT OUTER JOIN syscomments e ON b.cdefault = e.id
    32. 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)
    33. Where (a.id=b.id)AND(a.id=object_id('要查询的表')) --要查询的表改成你要查询表的名称
    34. orDER BY b.colid
    35. 程序代码
    36. Select
    37. 表名=case when a.colorder=1 then d.name else '' end,
    38. 表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,
    39. 字段序号=a.colorder,
    40. 字段名=a.name,
    41. 标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then '√' else '' end,
    42. 主键=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,
    43. 类型=b.name,
    44. 字段长度=a.length,
    45. 占用字节数=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
    46. 小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
    47. 允许空=case when a.isnullable=1 then '√'else '' end,
    48. 默认值=isnull(e.text,''),
    49. 字段说明=isnull(g.[value],'')
    50. FROM syscolumns a
    51. left join systypes b on a.xusertype=b.xusertype
    52. inner join sysobjects d on (a.id=d.id)and(d.xtype='U')and(d.name<>'dtproperties')
    53. left join syscomments e on a.cdefault=e.id
    54. left join sysproperties g on (a.id=g.id)and(a.colid=g.smallid)
    55. left join sysproperties f on (d.id=f.id)and(f.smallid=0)
    56. --where d.name='要查询的表' --如果只查询指定表,加上此条件
    57. order by a.id,a.colorder

--SQL SERVER 2000中各表外键名,主键名的获取SELECT 外键表ID = b.fkeyid , 外键表名称 =object_name(b.fkeyid) , 外键列ID = b.fkey , 外键列名 = (SELECT name FROM syscolumns WHERE colid = b.fkey AND id = b.fkeyid) , 主键表ID = b.rkeyid , 主键表名 =object_name(b.rkeyid) , 主键列ID = b.rkey , 主键列名 = (SELECT name FROM syscolumns WHERE colid = b.rkey AND id = b.rkeyid) , 级联更新 =ObjectProperty(a.id,'CnstIsUpdateCascade') , 级联删除 =ObjectProperty(a.id,'CnstIsDeleteCascade') FROM sysobjects a join sysforeignkeys b on a.id = b.constid join sysobjects c on a.parent_obj = c.id where a.xtype ='f'AND c.xtype ='U'

人气教程排行