增加字段
alter table docdsp add dspcode char(200)
删除字段
ALTER TABLE table_NAME DROP COLUMN column_NAME
修改字段类型
ALTER TABLE table_name ALTER COLUMN column_name new_data_type
改名
sp_rename
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
语法
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]
/**************************************************************************************/
判断table1中是否存在name字段
if exists(select * from syscolumns where id=object_id('table1') and name='name') begin
select * from people;
end
========================================================================
如果是实表可以用
if exists (select * from sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[表名]--如果表存在就删除
如果是临时表可以用
if object_id('tempdb..##temp') is not null
drop table ##temp
==========================================================
得到表字段的描述
一般用这个视图
Create view fielddesc
as
select o.name as oname, c.name as cname,convert(varchar(30),p.value) as value,p.smallid as psmallid,t.name as tname
from syscolumns c
join systypes t on c.xtype = t.xtype
join sysobjects o on o.id=c.id
left join sysproperties p on p.smallid=c.colid and p.id=o.id
where o.xtype='U'
查询时:
Select * from fielddesc where oname = '你的表名'