取得SQL SERVER 中一些信息的方法。

来源:互联网 发布:node.js教程 编辑:程序博客网 时间:2024/06/04 19:05

select name from sysobjects order by crdate   //检索数据库中的对象名称
select * from sysobjects where objectproperty(id, 'isusertable')=1 //查询所有的用户表

select syscolumns.[name] from syscolumns,sysobjects
where syscolumns.[id]=sysobjects.[id] and sysobjects.[name]='Orders' // 检索表中的字段名

exec sp_pkeys @table_name = 'ctlm05' // 获取表中的主键列

sp_columns 'orders' // 获取所有字段的字段类型

alter table a alter column id dec(10,1)   //更改表a中得id字段为dec(10,1)类型 

 

SELECT 
 
[TableName]=case when a.colorder=1 then d.name else '' end,
 
[ColOrder]=a.colorder,
 
[ColName]=a.name,
 
[IsIdentity]=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then ''else '' end,
 
[IsPK]=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,
 
[TypeName]=b.name,
 
[Size]=a.length,
 
[Lenth]=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
 
[DecimalPlace]=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
 
[CanBeNull]=case when a.isnullable=1 then ''else '' end,
 
[DefaultValue]=isnull(e.text,'')--,
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 not in ('dtproperties','sysdiagrams')
 
left join syscomments e on a.cdefault=e.id
order by a.id,a.colorder