這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Sql Server中怎么判斷表、列不存在則創(chuàng)建,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
第一種方法
IF EXISTS ( SELECT 1 FROM SYSOBJECTS T1 INNER JOIN SYSCOLUMNS T2 ON T1.ID=T2.ID WHERE T1.NAME='A' AND T2.NAME='C' ) PRINT '存在' ELSE PRINT '不存在'
第二種方法,短小精悍,可謂精典
IF COL_LENGTH('A', 'C') IS NOT NULL PRINT N'存在' ELSE PRINT N'不存在'
方法一:
select * from syscolumns where id=object_id('表名') and name='列名'
說(shuō)明:存在則返回此列的一條說(shuō)明記錄,不存在返回空;
方法二:
select count(*) from sysobjects a,syscolumns b where a.id=b.id and b.name='flag1' and a.type='u' and a.name='T_Pro_ProductClass'
說(shuō)明:存在返回1,不存在則返回0
二、Sql Server中判斷表、列是否存在,如果不存在則創(chuàng)建
一、表不存在則創(chuàng)建:
if not exists (select * from sysobjects where id = object_id('mytab') and OBJECTPROPERTY(id, 'IsUserTable') = 1)create table mytab( id int, age int , name varchar(max), primary key (id,age))go
二、列不存在則創(chuàng)建。
if not exists (select * from syscolumns where id=object_id('mytab') and name='columnname') alter table [mytab] add columnname nvarchar(max)
上述就是小編為大家分享的Sql Server中怎么判斷表、列不存在則創(chuàng)建了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前標(biāo)題:SqlServer中怎么判斷表、列不存在則創(chuàng)建-創(chuàng)新互聯(lián)
文章分享:http://vcdvsql.cn/article14/hdode.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站排名、小程序開發(fā)、靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容