如果需要判斷添加列的表中是否有主鍵,或者是判斷欄位是否存在,應該怎麼做呢?下面就為您介紹實現該功能的SQL語句寫法,供您參考學習。
AD:
下文為您介紹的SQL語句可以實現判斷欄位是否存在,並判斷添加列的表中是否有主鍵,這些SQL語句比較有實用的價值,希望可以讓您對SQL語句有更多的認識。
- --判斷要添加列的表中是否有主鍵
- if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')
- begin
- print '表中已經有主鍵,列只能做為普通列添加'
-
- --添加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
-
- 判斷table1中是否存在name欄位且刪除欄位
- if exists(select * from syscolumns where id=object_id('table1') and name='name') begin
- select * from people;
- alter table table1 DROP COLUMN name
- end
【編輯精選】
表添加欄位的SQL語句寫法
SQL Xml欄位的修改方法
查詢XML類型資料的SQL語句
SQL定義Xml欄位
Sql Server表相關的語句