MSSQL 數據庫表字段增/刪/改/查的操作
來源:程序員人生 發布時間:2014-05-23 16:21:24 閱讀次數:3909次
添加
1、向Loginfo中添加新列“newcomlum1” ,int型,
ALTER TABLE Loginfo ADD newcomlum1 int NULL
2、表:LogInfo
代碼
--判斷要添加列的表中是否有主鍵
if exists(select 1 from sysobjects where parent_obj=object_id('LogInfo') and xtype='PK')
begin
print '表中已經有主鍵,列只能做為普通列添加'
--添加int類型的列,默認值為0
alter table LogInfo add 列名 int default 0
end
else
begin
print '表中無主鍵,添加主鍵列'
--添加int類型的列,默認值為0
alter table LogInfo add 列名 int primary key default 0
end