动态SQL中不同变量的写法总结
1.一般变量的写法:
if (str_kind is not null) then
l_str_kind := str_kind;
v_wheresql := v_wheresql || ' and kind = :kind ';
else
l_str_kind := '';
v_wheresql := v_wheresql || ' and 1 = :kind ';
end if; 2.时间段的写法:
if (dt_itstarttime is not null) then
v_wheresql := v_wheresql || ' and createtime >= (' || to_date(dt_itstarttime, 'yyyy-mm-dd hh24:mi:ss') || ' )';
end; if (dt_itstarttime is not null) then
v_wheresql := v_wheresql || ' and createtime <= (' || to_date(dt_itstarttime, 'yyyy-mm-dd hh24:mi:ss') || ' )';
end; if (dt_valstarttime is not null) then
v_wheresql := v_wheresql || ' and validtime >= (' || to_date(dt_valstarttime, 'yyyy-mm-dd hh24:mi:ss') || ' )';
end; if (dt_valendtime is not null) then
v_wheresql := v_wheresql || ' and validtime <= (' || to_date(dt_valendtime, 'yyyy-mm-dd hh24:mi:ss') || ' )';
end; 或:
v_sql := v_sql || ' and trunc(a.creattime) >= to_date(:crestarttime,''yyyy-mm-dd'') ';
v_sql := v_sql || ' and trunc(a.creattime) <= to_date(:creendtime,''yyyy-mm-dd'') ';
v_sql := v_sql || ' and trunc(a.updatetime) >= to_date(:updstarttime,''yyyy-mm-dd'') ';
v_sql := v_sql || ' and trunc(a.updatetime) <= to_date(:updendtime,''yyyy-mm-dd'') '; 3.时间的写法:
v_sql := v_sql || ' and logtime >= to_date(:logtime, '|| ' ''yyyy-mm-dd hh24:mi:ss'')'; 4.or的写法:
if (str_object is not null) then
l_str_object := str_object;
v_sql := v_sql || ' and ( objectid = :objectid or objectname = :objectname )';
else
l_str_object := 1;
v_sql := v_sql || ' and ( 1 = :objectid or 1 = :objectname ) ';
end if; 注意: 此时在using 中l_str_object要写两个 5.字符串的写法:('|| 字符串 ||')
举例:
--1.trim和rtrim
v_sql := v_sql || ' and status in (' || ltrim(rtrim(str_status, ','), ',') || ') ';
--2.字符串
v_sql := ' select netname from t_cms_netconf where netid = ' || str_netid || ' order by 1 ';
--3.in
v_sql := ' t.status in (' || rtrim(str_status, ',') || ')'
--4.instr
v_sql := v_sql || ' and instr(''|'' || b.typelist || ''|'', ''|'' || :typelist || ''|'') > 0'; 6.数字的写法: ||数字
举例: v_sql := v_sql || ' and handletimes < ' || f_ums_config_in_qr('deploy/cms/task/maxsend', 5); 7.数组的写法:
sql中的写法:returning transactionid into :arr_tranactionid
using中的写法:returning bulk collect into v_arr_tranactionid --v_arr_tranactionid定义的临时变量 8.like的用法及%和_的处理
--1.
if (str_specialname is not null) then
l_str_specialname := '%' || lower(str_specialname) || '%';
v_sql := v_sql || ' and lower(a.specialname) like :specialname ';
else
l_str_specialname := '';
v_sql := v_sql || ' and 1 = :specialname ';
end if; if (str_tapecopyright is not null) then
l_str_tapecopyright := '%' || lower(str_tapecopyright) || '%';
v_sql := v_sql || ' and lower(a.tapecopyright) like :tapecopyright ';
else
l_str_tapecopyright := '';
v_sql := v_sql || ' and 1 = :tapecopyright ';
end if; --2.
if (str_specialname is not null) then
l_str_specialname := '%' || replace(replace(lower(str_specialname), '%', '<%'), '_', '<_') || '%';
v_sql := v_sql || ' and lower(a.specialname) like :specialname escape ''<'' ';
else
l_str_specialname := '';
v_sql := v_sql || ' and 1 = :specialname ';
end if; if (str_tapecopyright is not null) then
l_str_tapecopyright := '%' || replace(replace(lower(str_tapecopyright), '%', '<%'), '_', '<_') || '%';
v_sql := v_sql || ' and lower(a.tapecopyright) like :tapecopyright escape ''<'' ';
else
l_str_tapecopyright := '';
v_sql := v_sql || ' and 1 = :tapecopyright ';
end if; --经测试,两者写法均可. 9.页面上选择"按时间排序"的数据库处理方法
--按修改时间排序. 1-时间顺序,2-时间倒序
if ( i_sortorder = 2 ) then
v_sql := v_sql || ' order by a.createtime desc, a.copyrightid ';
else
v_sql := v_sql || ' order by a.createtime asc, a.copyrightid ';
end if;
动态SQL中不同变量的写法总结的更多相关文章
- mybatis在动态 SQL 中使用了参数作为变量,必须要用 @Param 注解
如果在动态 SQL 中使用了参数作为变量,那么就要用 @Param 注解,即使你只有一个参数.如果我们在动态 SQL 中用到了 参数作为判断条件,那么也是一定要加 @Param 注解的,例如如下方法: ...
- 在PL/SQL中使用游标、动态sql和绑定变量的小例子
需求:查询并输出30号部门的雇员信息 方式一:使用 loop...fetch SET serveroutput ON; DECLARE CURSOR c_emp IS ; v_emp emp%rowt ...
- 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的两个内置参数 不只是方法传递过来的参数可以被 ...
- 动态SQL中变量赋值
在动态SQL语句中进行变量的值绑定比较麻烦,这儿做个记录 declare @COUNT int,@sql nvarchar(max) set @sql = 'select @COUNT = count ...
- 关于动态SQL中的NULL
declare v_sql ); v_c1 number; v_c2 number; begin v_c2 :; v_sql := 'begin '; v_sql := v_sql||'update ...
- mybatis动态SQL中的sql片段
在mybatis中通过使用SQL片段可以提高代码的重用性,如下情景: 1.创建动态SQL <sql id="sql_count">select count(*)< ...
- PL/SQL中字符串变量的分割转化
在编写PL/SQL时,有时候我们需要处理这样一个输入的变量,它的格式是由多个值通过分隔符组成的字符串,如“1,2,3”,我们需要将这个变量加入到我们的SQL中,形成诸如in('1','2','3')的 ...
随机推荐
- Python系列-python文件操作
原链接:https://blog.csdn.net/m0_37745438/article/details/79573414 python提供了一系列方法来对文件进行读取.写入等操作 一.打开文件的方 ...
- RTSP连接中断重连的问题
最近在调查的一个问题. 起因是我司的一款数据链产品,15km数字图传,测试时发现视频画面经常会出现马赛克或卡顿. 图传设置了10Mbps速率,而视频码流是4Mbps,按道理不应该出现这种问题. 经过几 ...
- jQuery ajax方法success()中后台传来的四种数据类型
1.后台返回一个页面 js代码 /**(1)用$("#content-wrapper").html(data);显示页面*/ $.ajax({ async : false, cac ...
- php程序报错:PHP Core Warning/cannot open shared object file: No such file or directory
今天开发调试程序的时候报错了,现象是有时候刷新会出现如下图: 这种主要是找不到共享库文件,即.so文件,网上主要有3种解决方法: 1. 用ln将需要的so文件链接到/usr/lib或者/lib这两个默 ...
- Android学习——NDK交叉编译
原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 一. 环境1.GNU/Linux Ubuntu12.04操作系统(x86) 二. 下载NDK安装 ...
- Hadoop MR编程
Hadoop开发job需要定一个Map/Reduce/Job(启动MR job,并传入参数信息),以下代码示例实现的功能: 1)将一个用逗号分割的文件,替换为“|”分割的文件: 2)对小文件合并,将文 ...
- Struts(十一):OGNL表达式(二)
Map栈 :request,session,application的一个属性值或一个请求参数的值. 若想访问ContextMap里的某个对象的属性,可以使用以下几种之一: #object.proper ...
- X5 Blink下文字自动变大
在X5 Blink中,页面排版时会主动对字体进行放大,会检测页面中的主字体,当某一块的字体在我们的判定规则中,认为字体的字号较小,并且是页面中的主要字体,就会采用主动放大的操作.这显然不是我们想要的. ...
- 不用第三方解码库取得图片宽高 附完整C++算法实现代码
在特定的应用场景下,有时候我们只是想获取图片的宽高, 但不想通过解码图片才取得这个信息. 预先知道图片的宽高信息,进而提速图片加载,预处理等相关操作以提升体验. 在stackoverflow有一篇相关 ...
- 2018年Java后端面试经历
楼主16年毕业,16年三月份进入上一家公司到今年3月底,所以这是一份两年工作经验面经分享. 都说金三银四,往些年都是听着过没啥特别的感觉.今年自己倒是确确实实体验了一把银四,从3月26裸辞到4月17号 ...