动态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')的 ...
随机推荐
- 从PRISM开始学WPF(九)交互Interaction?
0x07交互 这是这个系列的最后一篇了,主要介绍了Prism中为我们提供几种弹窗交互的方式. Notification通知式 Prism通过InteractionRequest 来实现弹窗交互,它是一 ...
- hdu 1880 魔咒字典
https://vjudge.net/problem/HDU-1880 题意:略 思路: 一开始就是想到了正确的思路,但是代码写炸了,死活过不了.这题嘛,就是建议一个魔咒与咒语的双向映射.首先用字符串 ...
- Canvas绘制五角星
from tkinter import * import math as m root = Tk() w = Canvas(root, width=200, height=100, backgroun ...
- JS中的递归
递归基础 递归的概念 在程序中函数直接或间接调用自己 直接调用自己 简介调用自己 跳出结构,有了跳出才有结果 递归的思想 递归的调用,最终还是要转换为自己这个函数 如果有个函数foo,如果他是递归 ...
- OpenGL 背面剔除
在OpenGL种可使用glEnable(GL_CULL_FACE)开启背面剔除功能,即把那些我们看不见的面删除.但在剔除之前我们需要定义正面和背面,这个可以用法线来理解.在数学学科中,法线是用右手法则 ...
- 列表(list)之一定义 添加 删除 排序 反转 索引等其他操作
1.定义: 创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可,序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. # 列表list1=[&q ...
- 医疗器械c#上位机开发指引教程
此教程面向的读者:对医疗器械上位机编程有兴趣,或者急需了解医疗器械(尿常规.血液分析.生化.心电.B超等医疗下位仪器)的编程流程.编程细节的程序员. 1.得到仪器协议 当我们需要与医疗器械等下位机数据 ...
- [BZOJ 3456]城市规划
Description 题库链接( bzoj 权限题,可以去 cogs 交♂ 题库链接2 求含有 \(n\) 个点有标号的简单无向联通图的个数.方案数对 \(1004535809(479\times ...
- [BZOJ1977]严格次小生成树
[问题描述] 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等. 正当小C洋洋得意之时,小P又来泼小C冷水了.小P说,让小C求出一个无向图的次小生成树,而且这个次小生成 ...
- ●POJ 1269 Intersecting Lines
题链: http://poj.org/problem?id=1269 题解: 计算几何,直线交点 模板题,试了一下直线的向量参数方程求交点的方法. (方法详见<算法竞赛入门经典——训练指南> ...