zoukankan      html  css  js  c++  java
  • MySql带参数的存储过程编写(动态执行SQL语句)

    该存储过程是根据用户输入的条件和排序方式查询用户的信息,排序条件可以没有
    调用方式:call GetUsersDynamic('age<=30','');

    /********动态查询用户的信息********/
    CREATE PROCEDURE GetUsersDynamic(WhereCondition varchar(
    500),OrderByExpress varchar(100))
    begin
    declare stmt varchar(
    2000);
    if LENGTH(OrderbyExpress)>0 then
    begin
         set @sqlstr
    =concat('select id,name,password,age,getdate(adddate) as AddDate from users where ',WhereCondition,' order by ',OrderByExpress);
    end;
    else
    begin
         set @sqlstr
    =concat('select id,name,password,age,getdate(adddate) as AddDate from users where ',WhereCondition);
    end;
    end 
    if;
    prepare stmt from @sqlstr;
    execute stmt;
    end;
     
    getdate()是一个自定义的函数,作用是返回日期的短格式
    CREATE DEFINER=`root`@`localhost` FUNCTION `getdate`($date datetime) RETURNS varchar(50) CHARSET latin1
    return date_format($date,'%Y-%m-%d');

    动态插入数据的存储过程,(注意四个单引号表示一个一引号):


    Code
  • 相关阅读:
    Spring实现声明式事务
    Spring整合MyBatis
    Spring AOP
    代理模式
    Bean的作用域
    Spring的配置
    HQL题目记录以及解题思路--持续更新
    数仓学习之路一:数仓理论
    DBeaver连接Hive遇到的坑
    MySQL常见面试题
  • 原文地址:https://www.cnblogs.com/ringwang/p/1241676.html
Copyright © 2011-2022 走看看