SQL語句增加列、修改列、刪除列 1.增加列: 1)alter table tableName add columnName varchar(30) 2)ALTER TABLE dbo.doc_exa ADD column_b VARCHAR(20) NULL, column_c INT NULL ; 2. 修改列類型: 1)alter table tableName alter column columnName varchar(4000)
3.修改列的名稱: 語法:sp_rename [ @objname = ]'object_name' , [ @newname = ] 'new_name' [ , [ @objtype =] 'object_type' ] 如:EXEC sp_rename 'oldname','PartStock' 1)EXEC sp_rename 'tableName.column1' , 'column2' (把表名為tableName的column1列名修改為column2) 4.刪除列:
1)alter table tableName drop column columnName
2)alter table tableName drop column columnName1,columnName2
5.指定表中某列默認數據
1)alter table dbo.doc_exz add constraint col_b_def default 50 for column_b
下面的示例將 TerritoryID 表中的 Sales.SalesTerritory 列重命名為 TerrID。 將以下示例復制並粘貼到查詢窗口中,然后單擊“執行”
USE AdventureWorks2012; GO EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN'; GO
出處:https://www.cnblogs.com/net-sky/p/11076256.html
https://blog.csdn.net/suntanyong88/article/details/6546913
=======================================================================================
SQL代碼示例 use asm ——–刪除字段 if exists (select * from syscolumns where id =object_id(‘tb_log’) and name=‘COLUMN1′) ALTER TABLE [tb_log ] DROP COLUMN [COLUMN1] if exists (select * from syscolumns where id =object_id(‘tb_log’) and name=‘COLUMN2′) ALTER TABLE [tb_log ] DROP COLUMN [COLUMN2] ———添加字段 alter table tb_log add COLUMN5 nvarchar(200), COLUMN6 int
