关于动态SQL中的NULL
declare
v_sql varchar2(4000);
v_c1 number;
v_c2 number;
begin
v_c2 := 999;
v_sql := 'begin ';
v_sql := v_sql||'update te1 set c1='||v_c1||', c2='||v_c2||' where c1=1 and c2=1;';
v_sql := v_sql||' if sql%notfound then ';
v_sql := v_sql||'begin ';
v_sql := v_sql||'insert into te1(c1,c2) values('||v_c1||','||v_c2||');';
v_sql := v_sql||'end;';
v_sql := v_sql||'end if;';
v_sql := v_sql||'end;';
execute immediate v_sql;
end;
/
以上代码因v_c1为null,会抛出一个ORA-00936(失效的表达式)的错误,做了几次试验,发现问题的所在,将代码改一下,把最终的动态SQL显示出来:
set serveroutput on
declare
v_sql varchar2(4000);
v_c1 number;
v_c2 number;
begin
v_c2 := 999;
v_sql := 'begin ';
v_sql := v_sql||'update te1 set c1='||v_c1||', c2='||v_c2||' where c1=1 and c2=1;';
v_sql := v_sql||' if sql%notfound then ';
v_sql := v_sql||'begin ';
v_sql := v_sql||'insert into te1(c1,c2) values('||v_c1||','||v_c2||');';
v_sql := v_sql||'end;';
v_sql := v_sql||'end if;';
v_sql := v_sql||'end;';
--execute immediate v_sql;
dbms_output.put_line(v_sql);
end;
/
执行后输出
begin
update te1
set c1 =, c2 = 999
where c1 = 1
and c2 = 1;
if sql%notfound then
begin
insert into te1 (c1, c2) values (, 999);
end;
end if;
end;
从输出的结果中可以看出,update中的c1=后面没有值,insert into中的values后也缺少值,由此可以看出,当变量值为null时,则传入为空,而不是实际的null,因此整个语句不完整,会报ORA-00936的错误。
修正:为变量加一个nvl函数
declare
v_sql varchar2(4000);
v_c1 number;
v_c2 number;
begin
v_c2 := 999;
v_sql := 'begin ';
v_sql := v_sql||'update te1 set c1='||nvl(v_c1,0)||', c2='||v_c2||' where c1=1 and c2=1;';
v_sql := v_sql||' if sql%notfound then ';
v_sql := v_sql||'begin ';
v_sql := v_sql||'insert into te1(c1,c2) values('||nvl(v_c1,0)||','||v_c2||');';
v_sql := v_sql||'end;';
v_sql := v_sql||'end if;';
v_sql := v_sql||'end;';
execute immediate v_sql;
end;
/
关于动态SQL中的NULL的更多相关文章
- mybatis动态sql中where标签的使用
where标记的作用类似于动态sql中的set标记,他的作用主要是用来简化sql语句中where条件判断的书写的,如下所示: <select id="selectByParams&qu ...
- MyBatis动态SQL中trim标签的使用
My Batis 官方文档 对 动态SQL中使用trim标签的场景及效果介绍比较少. 事实上trim标签有点类似于replace效果. trim 属性 prefix:前缀覆盖并增加其内容 suffix ...
- mybatis动态sql中的两个内置参数(_parameter和_databaseId)
mybatis动态sql中的两个内置参数(_parameter和_databaseId) <!-- mybatis动态sql的两个内置参数 不只是方法传递过来的参数可以被 ...
- mybatis在动态 SQL 中使用了参数作为变量,必须要用 @Param 注解
如果在动态 SQL 中使用了参数作为变量,那么就要用 @Param 注解,即使你只有一个参数.如果我们在动态 SQL 中用到了 参数作为判断条件,那么也是一定要加 @Param 注解的,例如如下方法: ...
- SQL中的Null深入研究分析
SQL中的Null深入研究分析 虽然熟练掌握SQL的人对于Null不会有什么疑问,但总结得很全的文章还是很难找,看到一篇英文版的, 感觉还不错. Tony Hoare 在1965年发明了 null 引 ...
- 深入详解SQL中的Null
深入详解SQL中的Null NULL 在计算机和编程世界中表示的是未知,不确定.虽然中文翻译为 “空”, 但此空(null)非彼空(empty). Null表示的是一种未知状态,未来状态,比如小明兜里 ...
- SQL中的NULL值
除is [not] null之外,空值不满足任何查找条件.–如果null参与算术运算,则该算术表达式的值为null.–如果null参与比较运算,则结果可视为false.在SQL-92中可看成unkno ...
- 深入具体解释SQL中的Null
NULL 在计算机和编程世界中表示的是未知,不确定.尽管中文翻译为 "空", 但此空(null)非彼空(empty). Null表示的是一种未知状态.未来状态,比方小明兜里有多少钱 ...
- (转) SQL 中的 NULL 你真的懂了吗?【数据库|SQL】
注:转载自下面链接 https://blog.csdn.net/lnotime/article/details/104847946 SQL 中的 NULL (译自 NULL Values in SQL ...
随机推荐
- javaScript的select元素和option的相关操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Centos 6.5 搭建php环境(nginx+mariadb+php7)
1.mariaDb vim /etc/yum.repos.d/MariaDB.repo [mariadb] name = MariaDB baseurl = http://yum.mariadb.or ...
- php mkdir 创建多级目录实例代码
先介绍一下 mkdir() 这个函数 mkdir($path,0777,true); 第一个参数:必须,代表要创建的多级目录的路径. 第二个参数:设定目录的权限,默认是 0777,意味着最大可能的访问 ...
- web UI
Semantic不错的UI,代码非常详细
- synaptics驱动,插入USB鼠标禁用触控板注册表
Title:synaptics驱动,插入USB鼠标禁用触控板注册表 --2010-11-01 22:21 记得以前用过一个synaptics的驱动,自带有插入USB鼠标禁用触控板的选项的,有些没有自带 ...
- Ubuntu下Git服务端搭建
1安装git $ sudo add-apt-repository ppa:git-core/ppa $ sudo apt-get update $ sudo apt-get install git 测 ...
- Android 之Service
service是运行在后台的服务,你可以启动一个服务Service来播放音乐,或者记录你地理信息位置的改变,或者启动一个服务来运行并一直监听某种动作. 接下来分析一下service 的生命周期: 1: ...
- UNIQUE NullAble
一般情况 UNIQUE 不应该出现nullable的 但是如果我们要支持也是有办法的,就是写一个filter. https://msdn.microsoft.com/en-us/library/ms1 ...
- ural 1020 Rope
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...
- Set的并集
public static void main(String[] args) { Set<Long> old = new HashSet<>(); for (int i = 0 ...