网站首页 博客 SQL Server查询表的结构
查询所有表名:
s.e.l.e.c.t id,name from sysobjects w.h.e.r.e xtype='u'
查询表结构:
s.e.l.e.c.t COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE,COLUMN_DEFAULT from INFORMATION_SCHEMA.columns w.h.e.r.e TABLE_NAME='表名'
查询表是否存在:
s.e.l.e.c.t id,name from sysobjects w.h.e.r.e id = object_id('表名') and type = 'u'
s.e.l.e.c.t id,name from sys.tables w.h.e.r.e name='表名' and type = 'u'
查询字段是否存在:
s.e.l.e.c.t id,name from syscolumns w.h.e.r.e name='字段名' and objectproperty(id,'IsUserTable')=1 and object_name(id)='表名'
添加表:
create table 表名(
fid int,
DateCreated datetime,
Content nvarchar(MAX),
ReplierName nvarchar (500)
)
删除表:
d.r.o.p table 表名
添加字段:
alter table 表名 add DateCreated datetime
alter table 表名 add Content nvarchar(MAX)
alter table 表名 add ReplierName nvarchar (500)
alter table 表名 add fid int
修改字段:
alter table 表名 alter column DateCreated datetime
alter table 表名 alter column Content nvarchar(MAX)
alter table 表名 alter column ReplierName nvarchar (500)
alter table 表名 alter column fid int
删除字段:
alter table 表名 d.r.o.p column 字段名