`
javaPrimary
  • 浏览: 59460 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

mysql存储过程 动态SQL

阅读更多
建表语句:

CREATE TABLE `t_user_log` (

  `userID` int(11) unsigned DEFAULT NULL,

  `userName` varchar(40) DEFAULT NULL,

  `oprDate` datetime DEFAULT NULL,

  `oprContent` varchar(80) DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=utf8

/*!50100 PARTITION BY RANGE (year(oprDate))

(PARTITION part0 VALUES LESS THAN (201310) ENGINE = MyISAM,

PARTITION part1 VALUES LESS THAN (201311) ENGINE = MyISAM,

PARTITION part2 VALUES LESS THAN (201312) ENGINE = MyISAM,

PARTITION part3 VALUES LESS THAN (201401) ENGINE = MyISAM,

PARTITION part4 VALUES LESS THAN (201402) ENGINE = MyISAM,

PARTITION part5 VALUES LESS THAN (201403) ENGINE = MyISAM)


存储过程语句:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `p_batchAddUser`(in userName varchar(4000), in oprDate varchar(4000),in oprContent varchar(4000))
BEGIN
-- 定义变量
declare tmp_userName varchar(4000);
declare tmp_oprDate varchar(4000);
declare tmp_oprContent varchar(4000);
declare v_userName varchar(20);
declare v_oprDate varchar(20);
declare d_oprDate datetime;
declare v_oprContent varchar(20);
declare i_userName_num int;
declare i_oprDate_num int;
declare i_oprContent_num int;
declare v_sql varchar(6000);

set v_sql = 'insert into t_user_log(userID, userName, oprDate, oprContent) values(';
set tmp_userName = userName;
set tmp_oprDate = oprDate;
set tmp_oprContent = oprContent;

set i_userName_num = instr(tmp_userName, '|');
set i_oprDate_num = instr(tmp_oprDate, '|');
set i_oprContent_num = instr(tmp_oprContent, '|');

-- select concat(SUBSTRING(tmp_userName, 1, 5), '    ', SUBSTRING(tmp_userName, 7));

while (i_userName_num > 0 and tmp_userName is not null) do

    set v_userName = substring(tmp_userName, 1, i_userName_num - 1);
    set v_oprDate = subString(tmp_oprDate, 1, i_oprDate_num - 1);
    set v_oprContent = subString(tmp_oprContent, 1, i_oprContent_num - 1);
    set d_oprDate = str_to_date(v_oprDate, '%Y-%m-%d %H:%i:%s');
   
    set tmp_userName = substring(tmp_userName, i_userName_num + 1);
    set tmp_oprDate = substring(tmp_oprDate, i_oprDate_num + 1);
    set tmp_oprContent = substring(tmp_oprContent, i_oprContent_num + 1);
   
    set i_userName_num = instr(tmp_userName, '|');
    set i_oprDate_num = instr(tmp_oprDate, '|');
    set i_oprContent_num = instr(tmp_oprContent, '|');
   
    select concat(v_userName, '    ', d_oprDate, '    ', v_oprContent);
    SET v_sql = CONCAT(v_sql, i_userName_num, ',', '\"',v_userName,'\"', ',', '\"',d_oprDate, '\"', ',', '\"',v_oprContent,'\"', '),(');
end while;

select concat(tmp_userName, '    ', tmp_oprDate, '    ', tmp_oprContent);
SET v_sql = CONCAT(v_sql, i_userName_num, ',', '\"', tmp_userName,'\"', ',', '\"',tmp_oprDate,'\"', ',', '\"',tmp_oprContent,'\"', ');');
select concat(v_sql);

-- 执行动态语句
set @v_sql = v_sql;
prepare stmt1 from @v_sql;
execute stmt1;
deallocate prepare stmt1;

END
0
0
分享到:
评论

相关推荐

    mysql存储过程 在动态SQL内获取返回值的方法详解

    本篇文章是对mysql存储过程在动态SQL内获取返回值进行了详细的分析介绍,需要的朋友参考下

    MySQL 存储过程中执行动态SQL语句的方法

    drop PROCEDURE if exists my_procedure; create PROCEDURE my_procedure() BEGIN declare my_sqll varchar(500);... 您可能感兴趣的文章:mysql 存储过程中变量的定义与赋值操作mysql存储过程详解mysq

    MySQL存储过程中实现执行动态SQL语句的方法

    本文实例讲述了MySQL存储过程中实现执行动态SQL语句的方法。分享给大家供大家参考。具体实现方法如下: mysql> mysql> delimiter $$ mysql> mysql> CREATE PROCEDURE set_col_value -> (in_table VARCHAR(128), -...

    mysql通用存储过程sql脚本

    mysql通用存储过程sql脚本,支持多表,子查询,高级查询 分页

    MySQL存储过程实战SQL脚本

    内容概述:通过MySQL存储过程实战的例子,学会使用MySQL存储过程。包含以下内容: 创建无参存储过程、有参存储过程、IF-ELSE存储过程、WHILE循环存储过程、CASE-WHEN条件控制存储过程、REPEAT UNTIL循环存储过程、...

    mysql和SqlServer在存储过程上的区别.txt

    mysql和sqlserver都是数据库,但是两者之间有一些区别,那么它们在存储过程的开发上又有哪些区别呢?

    MySQL存储过程的异常处理方法

    本文实例讲述了MySQL存储过程的异常处理方法。分享给大家供大家参考。具体如下: mysql> mysql> delimiter $$ mysql> mysql> CREATE PROCEDURE myProc -> (p_first_name VARCHAR(30), -> p_last_name VARCHAR(30)...

    mysql存储过程之返回多个值的方法示例

    本文实例讲述了mysql存储过程之返回多个值的方法。分享给大家供大家参考,具体如下: mysql存储函数只返回一个值。要开发返回多个值的存储过程,需要使用带有INOUT或OUT参数的存储过程。咱们先来看一个orders表它的...

    Mysql、SqlServer、SysBase存储过程自动生成工具

    0、完全支持Mysql、SqlServer、SysBase等数据库自动生成存储过程 1、 基本完全代替编写存储过程的工作任务。 2、 让不会写存储过程的测试人员,也可顺利完成加压测试工作。 3、 让会写存储过程的测试人员,短时间内...

    SQL2005动态表无限级分类存储过程

    这个是修改其它作者单一无限级分类存储过程版,修改后为动态表无限级分类存储过程,共享给大家. 适合于sql 2005

    mysql存储过程教程

    NULL 博文链接:https://669341085.iteye.com/blog/800576

    mysql 查询存储过程的 sql 语句.docx

    MySQL 查询存储过程是一种存储在 MySQL 服务器中的 SQL 代码段,可以被多个客户端 多次调用。它们被用来简化复杂的 SQL 查询,执行常见的任务,以及实现数据逻辑。通过 存储过程,我们可以在一次操作中,执行多个 ...

    mysql 查询存储过程的 sql 语句.7z

    MySQL 查询存储过程是一种存储在 MySQL 服务器中的 SQL 代码段,可以被多个客户端 多次调用。它们被用来简化复杂的 SQL 查询,执行常见的任务,以及实现数据逻辑。通过 存储过程,我们可以在一次操作中,执行多个 ...

    MYSQL存储过程编程

    mysql存储过程编程教程:  MySQL 存储过程编程基础 -- 指南,基本语句,存储过程中的 SQL 和错误处理  创建 MySQL 存储过程程序 -- 事务处理,内建函数,存储过程函数和触发器  在应用程序中使用 MySQL 存储...

    Mysql关于使用存储过程设置sql_mode

    NULL 博文链接:https://yixinhu.iteye.com/blog/1534691

    MySQL存储过程完整版使用代码示例

    资源包中囊括了MySQL数据库中的存储过程的...该资源下所有内容都是本人的日常软件开发经验总结,对于初学者使用MySQL存储过程的程序员具有重要参考价值,问大家要10分是不过分的,用过就知道了,欢迎大家下载参考及使用

    MySQL创建存储过程批量插入10万条数据

    MySQL创建存储过程批量插入10万条数据 存储过程 1、首先防止主键冲突,我们清空表。 TRUNCATE table A_student; 2、编写存储过程 delimiter ‘$’; CREATE PROCEDURE batchInsert(in args int) BEGIN declare i int ...

    SQL存储过程实例.doc

    --1、创建存储过程-- if exists (select * from sysobjects where name='Sum_wage') drop procedure Sum_wage GO create procedure Sum_wage @PWage int, @AWage int, @total int as while (1=1) begin if (select...

    SQL导出MySQL单个存储过程.txt

    SQL语句对单个的MySQL存储过程导出 首先确保参数secure-file-priv='' 表示不限制mysqld在任意目录的导入导出 打开my.cnf或my.ini,加入如下语句后重启mysql服务。 secure_file_priv='' sql语法只修改下面的...

Global site tag (gtag.js) - Google Analytics