当判断的表不存时,我可以执行创建数据库,创建表,增加列,可以执行相应的SQL语句;
而if exists同理判断,首先判断查询结果是否存在,如果存在执行判断后面的语句,查询的数据库,表,列的方法相同;
mssql语法:
代码如下:
if not exists (SELECT 1 FROM [t_Table] where [fName] = '张三')
insert into [t_Table] ([fName]) values ('张三');
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] = '张三');
insert into [t_Table] ([fName]) select '张三'
where not exists (SELECT 1 FROM [t_Table] where [fName] = '张三');
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。