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中不同变量的写法总结的更多相关文章

  1. mybatis在动态 SQL 中使用了参数作为变量,必须要用 @Param 注解

    如果在动态 SQL 中使用了参数作为变量,那么就要用 @Param 注解,即使你只有一个参数.如果我们在动态 SQL 中用到了 参数作为判断条件,那么也是一定要加 @Param 注解的,例如如下方法: ...

  2. 在PL/SQL中使用游标、动态sql和绑定变量的小例子

    需求:查询并输出30号部门的雇员信息 方式一:使用 loop...fetch SET serveroutput ON; DECLARE CURSOR c_emp IS ; v_emp emp%rowt ...

  3. mybatis动态sql中where标签的使用

    where标记的作用类似于动态sql中的set标记,他的作用主要是用来简化sql语句中where条件判断的书写的,如下所示: <select id="selectByParams&qu ...

  4. MyBatis动态SQL中trim标签的使用

    My Batis 官方文档 对 动态SQL中使用trim标签的场景及效果介绍比较少. 事实上trim标签有点类似于replace效果. trim 属性 prefix:前缀覆盖并增加其内容 suffix ...

  5. mybatis动态sql中的两个内置参数(_parameter和_databaseId)

    mybatis动态sql中的两个内置参数(_parameter和_databaseId)   <!-- mybatis动态sql的两个内置参数           不只是方法传递过来的参数可以被 ...

  6. 动态SQL中变量赋值

    在动态SQL语句中进行变量的值绑定比较麻烦,这儿做个记录 declare @COUNT int,@sql nvarchar(max) set @sql = 'select @COUNT = count ...

  7. 关于动态SQL中的NULL

    declare v_sql ); v_c1 number; v_c2 number; begin v_c2 :; v_sql := 'begin '; v_sql := v_sql||'update ...

  8. mybatis动态SQL中的sql片段

    在mybatis中通过使用SQL片段可以提高代码的重用性,如下情景: 1.创建动态SQL <sql id="sql_count">select count(*)< ...

  9. PL/SQL中字符串变量的分割转化

    在编写PL/SQL时,有时候我们需要处理这样一个输入的变量,它的格式是由多个值通过分隔符组成的字符串,如“1,2,3”,我们需要将这个变量加入到我们的SQL中,形成诸如in('1','2','3')的 ...

随机推荐

  1. nginx配置反向代理详细教程(windows版)

    内容属于原创,如果需要转载,还请注明地址:http://www.cnblogs.com/j-star/p/8785334.html Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(I ...

  2. VirtualBox的共享文件夹功能的使用演示

    演示环境 虚拟机 Oracle VM VirtualBox 宿主机 Windows 客户机 Linux 以下图片演示中使用的Linux客户机为CentOS.对于Debian系统的客户机,主要在安装增强 ...

  3. SourceTree 01 - git 客户端介绍

    SourceTree - git客户端介绍 SourceTree系列第1篇 --->> SourceTree 01 - git 客户端介绍(http://www.cnblogs.com/g ...

  4. 泡菜的使用pickle

    如何实现对列表和字典的写入? 需要将对象流式化,实现对象持久存储,这里用到的事pickle 一.制作泡菜 >>> list1 = ['我',123,3.14,['aaa',1]] # ...

  5. Java中的变量数据类型补充

    Java中变量的数据类型的补充 变量按照数据类型进行分类 1.基本数据类型 数值型:①整数类型(byte,short,int,long)②浮点型(float,doubbe)③字符型(char)④布尔型 ...

  6. Maven 本地仓库明明有jar包,pom文件还是报错解决办法

    方法一: 找到出错的jar包文件位置,删掉_maven.repositories文件 方法二: maven中的本地仓库的index索引没有更新导致 解决方案: 在eclipse中打开菜单 window ...

  7. Java知识体系纲要

    最近一段时间,把Java主要涉及到的大概念都大致学习了一遍,为了让自己能够更好地形成对Java知识体系的整体把握,先把学过的知识点添加到自己画的思维导图上. 整个Java知识体系的划分,我自己主要将它 ...

  8. 使用Markup解析xml文件

    1:怎么获取Markup.cpp 和 Markup.h 首先到http://www.firstobject.com/dn_markup.htm链接下,下载Release 11.5 zip (579k) ...

  9. 用js来实现那些数据结构06(队列)

    其实队列跟栈有很多相似的地方,包括其中的一些方法和使用方式,只是队列使用了与栈完全不同的原则,栈是后进先出原则,而队列是先进先出(First In First Out). 一.队列    队列是一种特 ...

  10. [LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...