动态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 request
python request a. 客户端向服务端发送多层字典的值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 obj = ...
- POJ-1258 Agri-Net---MST裸题Prim
题目链接: https://vjudge.net/problem/POJ-1258 题目大意: 求MST 思路: 由于给的是邻接矩阵,直接prim算法 #include<iostream> ...
- [转]Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode character ' ...
- RTKLIB源码解析(一)——单点定位(pntpos.c)
RTKLIB源码解析(一)--单点定位(pntpos.c) 标签: GNSS RTKLIB 单点定位 [TOC] pntpos int pntpos (const obsd_t *obs, int n ...
- 解决:My97DatePicker 日期插件引用在PHP文件中maxDate和minDate控制失效问题
开发环境: 语言:PHP 框架:ThinkPHP 问题:在引用插件My97DatePicker时,想实现:开始日期不能大于结束日期,结束时间不能小于开始时间 步骤一.查看文档官方文档http://ww ...
- z-index的权重是叠加的
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 谁说深入浅出虚拟机难?现在我让他通俗易懂(JVM)
1:什么是JVM大家可以想想,JVM 是什么?JVM是用来干什么的?在这里我列出了三个概念,第一个是JVM,第二个是JDK,第三个是JRE.相信大家对这三个不会很陌生,相信你们都用过,但是,你们对这三 ...
- [LeetCode] Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- 【Swift】swift中懒加载的写法
swift中懒加载的写法,直接上例子 (懒加载一个遮罩视图) lazy var dummyView: UIView = { let v = UIView() v.backgroundColor = U ...
- clique
[题目描述]数轴上有 n 个点,第 i 个点的坐标为 xi,权值为 wi.两个点 i,j 之间存在一条边当且仅当 abs(xi-xj)>=wi+wj.你需要求出这张图的最大团的点数.(团就是两两 ...