您现在的位置是:网站首页> 编程资料编程资料
mssql和sqlite中关于if not exists 的写法_MsSql_
2023-05-26
487人已围观
简介 mssql和sqlite中关于if not exists 的写法_MsSql_
在sql语名中,if not exists 即如果不存在,if exists 即如果存在。
下面学习下二者的用法。
a,判断数据库不存在时
复制代码 代码如下:
if not exists(select * from sys.databases where name = 'database_name')
b,判断表不存在时
复制代码 代码如下:
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')
当判断的表不存时,我可以执行创建数据库,创建表,增加列,可以执行相应的SQL语句;
而if exists同理判断,首先判断查询结果是否存在,如果存在执行判断后面的语句,查询的数据库,表,列的方法相同;
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] = '张三');
您可能感兴趣的文章:
相关内容
- sql清空表数据后重新添加数据存储过程的示例_MsSql_
- 在SQL Server中查询资料库的TABLE数量与名称的sql语句_MsSql_
- 通过系统数据库获取用户所有数据库中的视图、表、存储过程_MsSql_
- 使用phpMyAdmin修改MySQL数据库root用户密码的方法_MsSql_
- 一段脚本实现自动备份并保存最近几天的SQL数据库_MsSql_
- sql查询表中根据某列排序的任意行语句_MsSql_
- sql将一个表中的数据插入到另一个表中的方法_MsSql_
- sql使用cast进行数据类型转换示例_MsSql_
- sql时间格式化输出、Convert函数应用示例_MsSql_
- sql多表行转列、级联行转列示例代码_MsSql_
