sql的转义字符单引号
在SQL中,我们都知道单引号 ' 表示字符串的开始和结束符号,如:
select * from students where name = '小明';
但如果字符串里面有单引号时,应该怎么查询呢?
这是我最近遇到的一个问题,需求是对一张表的数据进行更新,但各个环境的数据并不一致,只能通过拼接的方式生成适合对应环境的变更脚本。更新语句格式如下:
update students set grade = '一年级' where grade_id = '' and grade is null;
update students set grade = '二年级' where grade_id = '' and grade is null;
...
--只是举例,实际就是各个环境字段值不一致,需要先根据环境拼接变更脚本
拼接sql语句的脚本初始如下:
--db2数据库使用下面的语句
select 'update students set grade = ' || grade || ' where grade_id = ' || grade_id || ' and grade is null;' from classes
where grade_id in (select grade_id from students where grade_id is not null and grade is null); --mysql数据库使用下面的语句
select concat('update students set grade = ',grade,' where grade_id = ',grade_id,' and grade is null;') from classes
where grade_id in (select grade_id from students where grade_id is not null and grade is null);
结果如下:

可以发现,字符串值没有单引号,直接运行会报错。google后找到解决办法。sql单引号
sql的转义字符单引号 ' ,可以对字符串中的单引号进行转义,使其表示字符串值 ' ,这样如果要查询 name 为 小'明 的值,其sql语句如下:
select * from students where name = '小''明';
所以上面的拼接脚本修改如下,即可生成正确的update语句。
--db2数据库使用下面的语句
select 'update students set grade = ''' || grade || ''' where grade_id = ''' || grade_id || ''' and grade is null;' from classes
where grade_id in (select grade_id from students where grade_id is not null and grade is null); --mysql数据库使用下面的语句
select concat('update students set grade = ''',grade,''' where grade_id = ''',grade_id,''' and grade is null;') from classes
where grade_id in (select grade_id from students where grade_id is not null and grade is null); --注意三个逗号需连续,且与其他字符间的空格
sql的转义字符单引号的更多相关文章
- 浅谈SQL中的单引号
单引号:对很对计算机语言包括(SQL)是做字符串引用的:这个是大家通常知道的作用:但是对SQL语言来说:还有另外一个作用是作引号的转义 总结下:对oracle(sql)的作用. 做字符串引用:例如'a ...
- SQL中插入单引号,新增修改删除
1.插入单引号如果不转化的话,字符串插入到数据库中错误的,只要在字符串中有单引号的地方在加一个单引号即可. 例如:在数据库插入'井下设备' : insert into Static_Bel ...
- jackson json转实体 允许特殊字符和转义字符 单引号
//允许出现特殊字符和转义符 mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ; //允许出现单引号 mapper.confi ...
- Sql Server添加单引号
" ' "(单引号)的运用:在sql server中,两个" ' "(单引号)在拼接字符串的情况下运用,就是表示拼接上了一个" ' "单引号 ...
- sql字符串包含单引号
ad'min select * from user where name ='ad''min'
- SQL内模糊查询语句拼接时单引号'问题
下面以存储过程查询所有为例,非存储过程(或不是查询所有将*替换为你想要查询的列即可)更为简单, 语法:select * from 表名 where 列名like'%条件%' 拼接后的set @变量名 ...
- sql注入-推断是否存在SQL注入-单引号
来自:https://www.cnblogs.com/ichunqiu/p/5749347.html 首先我们需要了解数据是通过什么方式进行输入,这里我总结了三个: GET请求:该请求在URL中发送参 ...
- Java的sql语句 写关键字不需要添加单引号
Java的sql语句 写关键字不需要添加单引号
- js中单引号和双引号的区别(html中属性规范是用双引号,js中字符串规定是用单引号)(js中单引号区别和php很像:单引号快,双引号可转义字符,双引号可解析变量)
js中单引号和双引号的区别(html中属性规范是用双引号,js中字符串规定是用单引号)(js中单引号区别和php很像:单引号快,双引号可转义字符,双引号可解析变量) 一.总结 1.html中属性规范是 ...
随机推荐
- jquery操作select下拉框的多种方法(选中,取值,赋值等)
Query获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为Sel ...
- [精华][推荐] CAS SSO单点登录环境搭建及实例
1.因为是本地模拟sso环境,而sso的环境测试需要域名,所以需要虚拟几个域名出来,步骤如下: 2.进入目录C:\Windows\System32\drivers\etc 3.修改hosts文件 12 ...
- Render Functions & JSX
Render Functions & JSX Basics Vue recommends using templates to build your HTML in the vast majo ...
- Linux 安装源码软件
linux下,源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install) 过程中用到configure --prefix --with:其中--pr ...
- mysql 报错 Packets larger than max_allowed_packet are not allowed
登录 mysql, 执行命令 : show variables like '%max_allowed_packet%' 重新设置: set global max_allowed_packet = 1 ...
- VB编程中的“Abs”是什么意思?
c = Val(Text1.Text) '将Text1中的值赋给cIf c = Abs(a - b) Then 'Abs(a - b)是a和b间的差(正数),判断c是否等于该差值f = f + 10 ...
- 【SerpentAI:Python开源游戏智能体开发框架——相比OpenAI Universe可导入自己的游戏、可脱离Docker/VNC运行】
https://weibo.com/fly51fly?from=myfollow_all&is_all=1#1514439335614 [SerpentAI:Python开源游戏智能体开发框架 ...
- json解析Object
最近的工作是在数据库使用myBaties查出的数据没有实体, 比如: <select id="allTree" parameterType="String" ...
- Python之路系列笔记
备注:本套笔记内容来源于互联网,只做学习使用,如有侵权请联系本笔记作者. 资料内容 Python之路(一)——Python 初识 Python之路(二)——基础语法 Python之路(三)——函数 P ...
- Aes加解密,php
Aes类库 <?php namespace Aes; class Aes { /** * var string $method 加解密方法,可通过openssl_get_cipher_metho ...