1. 1.一般变量的写法:
  2. if (str_kind is not null) then
  3. l_str_kind := str_kind;
  4. v_wheresql := v_wheresql || ' and kind = :kind ';
  5. else
  6. l_str_kind := '';
  7. v_wheresql := v_wheresql || ' and 1 = :kind ';
  8. end if;
  9.  
  10. 2.时间段的写法:
  11. if (dt_itstarttime is not null) then
  12. v_wheresql := v_wheresql || ' and createtime >= (' || to_date(dt_itstarttime, 'yyyy-mm-dd hh24:mi:ss') || ' )';
  13. end;
  14.  
  15. if (dt_itstarttime is not null) then
  16. v_wheresql := v_wheresql || ' and createtime <= (' || to_date(dt_itstarttime, 'yyyy-mm-dd hh24:mi:ss') || ' )';
  17. end;
  18.  
  19. if (dt_valstarttime is not null) then
  20. v_wheresql := v_wheresql || ' and validtime >= (' || to_date(dt_valstarttime, 'yyyy-mm-dd hh24:mi:ss') || ' )';
  21. end;
  22.  
  23. if (dt_valendtime is not null) then
  24. v_wheresql := v_wheresql || ' and validtime <= (' || to_date(dt_valendtime, 'yyyy-mm-dd hh24:mi:ss') || ' )';
  25. end;
  26.  
  27. 或:
  28. v_sql := v_sql || ' and trunc(a.creattime) >= to_date(:crestarttime,''yyyy-mm-dd'') ';
  29. v_sql := v_sql || ' and trunc(a.creattime) <= to_date(:creendtime,''yyyy-mm-dd'') ';
  30. v_sql := v_sql || ' and trunc(a.updatetime) >= to_date(:updstarttime,''yyyy-mm-dd'') ';
  31. v_sql := v_sql || ' and trunc(a.updatetime) <= to_date(:updendtime,''yyyy-mm-dd'') ';
  32.  
  33. 3.时间的写法:
  34. v_sql := v_sql || ' and logtime >= to_date(:logtime, '|| ' ''yyyy-mm-dd hh24:mi:ss'')';
  35.  
  36. 4.or的写法:
  37. if (str_object is not null) then
  38. l_str_object := str_object;
  39. v_sql := v_sql || ' and ( objectid = :objectid or objectname = :objectname )';
  40. else
  41. l_str_object := 1;
  42. v_sql := v_sql || ' and ( 1 = :objectid or 1 = :objectname ) ';
  43. end if;
  44.  
  45. 注意: 此时在using l_str_object要写两个
  46.  
  47. 5.字符串的写法:('|| 字符串 ||')
  48. 举例:
  49. --1.trimrtrim
  50. v_sql := v_sql || ' and status in (' || ltrim(rtrim(str_status, ','), ',') || ') ';
  51. --2.字符串
  52. v_sql := ' select netname from t_cms_netconf where netid = ' || str_netid || ' order by 1 ';
  53. --3.in
  54. v_sql := ' t.status in (' || rtrim(str_status, ',') || ')'
  55. --4.instr
  56. v_sql := v_sql || ' and instr(''|'' || b.typelist || ''|'', ''|'' || :typelist || ''|'') > 0';
  57.  
  58. 6.数字的写法: ||数字
  59. 举例: v_sql := v_sql || ' and handletimes < ' || f_ums_config_in_qr('deploy/cms/task/maxsend', 5);
  60.  
  61. 7.数组的写法:
  62. sql中的写法:returning transactionid into :arr_tranactionid
  63. using中的写法:returning bulk collect into v_arr_tranactionid --v_arr_tranactionid定义的临时变量
  64.  
  65. 8.like的用法及%和_的处理
  66. --1.
  67. if (str_specialname is not null) then
  68. l_str_specialname := '%' || lower(str_specialname) || '%';
  69. v_sql := v_sql || ' and lower(a.specialname) like :specialname ';
  70. else
  71. l_str_specialname := '';
  72. v_sql := v_sql || ' and 1 = :specialname ';
  73. end if;
  74.  
  75. if (str_tapecopyright is not null) then
  76. l_str_tapecopyright := '%' || lower(str_tapecopyright) || '%';
  77. v_sql := v_sql || ' and lower(a.tapecopyright) like :tapecopyright ';
  78. else
  79. l_str_tapecopyright := '';
  80. v_sql := v_sql || ' and 1 = :tapecopyright ';
  81. end if;
  82.  
  83. --2.
  84. if (str_specialname is not null) then
  85. l_str_specialname := '%' || replace(replace(lower(str_specialname), '%', '<%'), '_', '<_') || '%';
  86. v_sql := v_sql || ' and lower(a.specialname) like :specialname escape ''<'' ';
  87. else
  88. l_str_specialname := '';
  89. v_sql := v_sql || ' and 1 = :specialname ';
  90. end if;
  91.  
  92. if (str_tapecopyright is not null) then
  93. l_str_tapecopyright := '%' || replace(replace(lower(str_tapecopyright), '%', '<%'), '_', '<_') || '%';
  94. v_sql := v_sql || ' and lower(a.tapecopyright) like :tapecopyright escape ''<'' ';
  95. else
  96. l_str_tapecopyright := '';
  97. v_sql := v_sql || ' and 1 = :tapecopyright ';
  98. end if;
  99.  
  100. --经测试,两者写法均可.
  101.  
  102. 9.页面上选择"按时间排序"的数据库处理方法
  103. --按修改时间排序. 1-时间顺序,2-时间倒序
  104. if ( i_sortorder = 2 ) then
  105. v_sql := v_sql || ' order by a.createtime desc, a.copyrightid ';
  106. else
  107. v_sql := v_sql || ' order by a.createtime asc, a.copyrightid ';
  108. 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. 从PRISM开始学WPF(九)交互Interaction?

    0x07交互 这是这个系列的最后一篇了,主要介绍了Prism中为我们提供几种弹窗交互的方式. Notification通知式 Prism通过InteractionRequest 来实现弹窗交互,它是一 ...

  2. hdu 1880 魔咒字典

    https://vjudge.net/problem/HDU-1880 题意:略 思路: 一开始就是想到了正确的思路,但是代码写炸了,死活过不了.这题嘛,就是建议一个魔咒与咒语的双向映射.首先用字符串 ...

  3. Canvas绘制五角星

    from tkinter import * import math as m root = Tk() w = Canvas(root, width=200, height=100, backgroun ...

  4. JS中的递归

      递归基础 递归的概念 在程序中函数直接或间接调用自己 直接调用自己 简介调用自己 跳出结构,有了跳出才有结果 递归的思想 递归的调用,最终还是要转换为自己这个函数 如果有个函数foo,如果他是递归 ...

  5. OpenGL 背面剔除

    在OpenGL种可使用glEnable(GL_CULL_FACE)开启背面剔除功能,即把那些我们看不见的面删除.但在剔除之前我们需要定义正面和背面,这个可以用法线来理解.在数学学科中,法线是用右手法则 ...

  6. 列表(list)之一定义 添加 删除 排序 反转 索引等其他操作

    1.定义: 创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可,序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. # 列表list1=[&q ...

  7. 医疗器械c#上位机开发指引教程

    此教程面向的读者:对医疗器械上位机编程有兴趣,或者急需了解医疗器械(尿常规.血液分析.生化.心电.B超等医疗下位仪器)的编程流程.编程细节的程序员. 1.得到仪器协议 当我们需要与医疗器械等下位机数据 ...

  8. [BZOJ 3456]城市规划

    Description 题库链接( bzoj 权限题,可以去 cogs 交♂ 题库链接2 求含有 \(n\) 个点有标号的简单无向联通图的个数.方案数对 \(1004535809(479\times ...

  9. [BZOJ1977]严格次小生成树

    [问题描述] 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等. 正当小C洋洋得意之时,小P又来泼小C冷水了.小P说,让小C求出一个无向图的次小生成树,而且这个次小生成 ...

  10. ●POJ 1269 Intersecting Lines

    题链: http://poj.org/problem?id=1269 题解: 计算几何,直线交点 模板题,试了一下直线的向量参数方程求交点的方法. (方法详见<算法竞赛入门经典——训练指南> ...