mssql和sqlite中關(guān)于if not exists 的寫法
在sql語名中,if not exists 即如果不存在,if exists 即如果存在。
下面學(xué)習(xí)下二者的用法。
a,判斷數(shù)據(jù)庫(kù)不存在時(shí)
if not exists(select * from sys.databases where name = 'database_name')
b,判斷表不存在時(shí)
if not exists (select * from sysobjects where id = object_id('table_name') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
c,判斷列不存在
if not exists (select * from syscolumns where id=object_id('table_name') and name='column_name')
當(dāng)判斷的表不存時(shí),我可以執(zhí)行創(chuàng)建數(shù)據(jù)庫(kù),創(chuàng)建表,增加列,可以執(zhí)行相應(yīng)的SQL語句;
而if exists同理判斷,首先判斷查詢結(jié)果是否存在,如果存在執(zhí)行判斷后面的語句,查詢的數(shù)據(jù)庫(kù),表,列的方法相同;
mssql語法:
if not exists (SELECT 1 FROM [t_Table] where [fName] = '張三')
insert into [t_Table] ([fName]) values ('張三');
sqlite語法:
insert into [t_Table] ([fName]) select '張三'
where not exists (SELECT 1 FROM [t_Table] where [fName] = '張三');
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信