SQL判斷字段列是否存在的方法
增加字段
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
更改當(dāng)前數(shù)據(jù)庫中用戶創(chuàng)建對象(如表、列或用戶定義數(shù)據(jù)類型)的名稱。
語法
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]
--假設(shè)要處理的表名為: tb
--判斷要添加列的表中是否有主鍵
if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')
begin
print '表中已經(jīng)有主鍵,列只能做為普通列添加'
--添加int類型的列,默認值為0
alter table tb add 列名 int default 0
end
else
begin
print '表中無主鍵,添加主鍵列'
--添加int類型的列,默認值為0
alter table tb add 列名 int primary key default 0
end
/**************************************************************************************/
判斷table1中是否存在name字段
if exists(select * from syscolumns where id=object_id('table1') and name='name') begin
select * from people;
end
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。