当前位置:首页 » 存储配置 » cmysql存储过程

cmysql存储过程

发布时间: 2022-10-16 00:56:33

㈠ mysql 存储过程

你应该在做统计吧,估计你不会的就是mysql存储过程的语法 我之前也写过 很是郁闷 我给你一段代码 是我用mysql写过的一个存储过程 你看看 主要是了解里面的语法 看懂了 你所说的需求并不难
有看不懂的地方一起讨论 :
begin

declare tikk datetime ;
declare done int default 0;
declare userid int default 0;
declare moleid int default 0;
declare couid int default 0;
declare mname varchar(255) ;
declare opsid int default 0;

declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status<>0 group by I_userID,I_operationID order by createtime desc;
declare continue handler for sqlstate '02000' set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select I_moleID from space_operation where status<>0 and ID=opsid into moleid;
if moleid <> '' then
select Nvc_identification from space_operation where status<>0 and ID=opsid into @identiftion;
if moleid > 0 then
Select Nvc_ename from space_mole where status<>0 and ID=moleid into mname;
else
set mname = 'space';
end if;

create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);
INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status<>0 and I_operationID=opsid and I_userID=userid ;
select count(*) from sp_tab1 into couid;

set @ihod = 0;
set @listp = '';
set @listpp = '';
set @content0p = '';
set @content0 = '';
while couid > 0 do
select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID > @ihod order by ID asc limit 0,1;
if @iok <> '' then
if mname = 'blog' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mname = 'team' then
if(@identiftion = 'addblog' || @identiftion = 'mdyblog') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = CONCAT(@listpp,CONCAT(@objiplk,','));
set @operarry1p = substring_index(@conuiy,'|',1);
set @operarry2p = substring_index(@conuiy,'|',-1);
set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1);
end if;
elseif mname = 'space' then
if(@identiftion = 'headphoto' || @identiftion = 'status') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = CONCAT(@listppr,CONCAT(@objiplk,','));
set @operarry1pr = substring_index(@conuiy,'|',1);
set @operarry2pr = substring_index(@conuiy,'|',-1);
set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1);
end if;
else
set @listp = CONCAT(@listp,CONCAT(@objiplk,','));
set @operarry1 = substring_index(@conuiy,'|',1);
set @operarry2 = substring_index(@conuiy,'|',-1);
set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));
set @objlist = substring(@listp,1,length(@listp)-1);
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;

if @content0 <> '' then
set @contentp = CONCAT(@operarry1,concat('|',@content0));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p <> '' then
if @identiftion = 'addphoto' then
set @contentp = CONCAT(@operarry1p,CONCAT('|',@content0p));
else
set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,'|'));
end if;

Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr <> '' then
set @contentp = CONCAT(@operarry1p,concat('|',@content0pr));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1;
end if;
end if;

until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;

UPDATE space_operation_play SET status=0;
UPDATE space_operation_display SET status=0;
Select createtime into @ptimes from space_operation_stat where status<>0 order by createtime desc limit 0,1;
if @ptimes <>'' then
create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);
INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status<>0 and createtime=@ptimes order by D_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id > 0 do
select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID > @uoj order by ID asc limit 0,1;
if @io1k <> '' then
INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while;
drop temporary table if exists sp_tab2 ;
end if;

end

㈡ mysql 存储过程 是什么意思

用select...into语句

下面是mysql 5.0的帮助文档的:
这个SELECT语法把选定的列直接存储到变量。因此,只有单一的行可以被取回。

SELECT id,data INTO x,y FROM test.t1 LIMIT 1;
注意,用户变量名在MySQL 5.1中是对大小写不敏感的。请参阅9.3节,“用户变量”。

重要: SQL变量名不能和列名一样。如果SELECT ... INTO这样的SQL语句包含一个对列的参考,并包含一个与列相同名字的局部变量,MySQL当前把参考解释为一个变量的名字。例如,在下面的语句中,xname 被解释为到xname variable 的参考而不是到xname column的:

CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE newname VARCHAR(5);
DECLARE xid INT;

SELECT xname,id INTO newname,xid
FROM table1 WHERE xname = xname;
SELECT newname;
END;
当这个程序被调用的时候,无论table.xname列的值是什么,变量newname将返回值‘bob’。

㈢ mysql 存储过程怎么调用

php调用mysql存储过程和函数的方法
存储过程和函数是MySql5.0刚刚引入的。关于这方面的操作在PHP里面没有直接的支持。但是由于Mysql PHP API的设计,使得我们可以在以前的PHP版本中的mysql php api中支持存储过程和函数的调用。

在php中调用存储过程和函数。

1。调用存储过程的方法。

a。如果存储过程有 IN/INOUT参数,声明一个变量,输入参数给存储过程,该变量是一对,

一个php变量(也可以不必,只是没有php变量时,没有办法进行动态输入),一个Mysql

变量。

b。如果存储过程有OUT变量,声明一个Mysql变量。

mysql变量的声明比较特殊,必须让mysql服务器知道此变量的存在,其实也就是执行一条mysql语句。

入set @mysqlvar=$phpvar ;

c。使用mysql_query()/mysql_db_query()执行mysql 变量声明语句。

mysql_query("set @mysqlvar=$pbpvar");

这样,在mysql服务器里面就有一个变量,@mysqlar。如果是IN参数,那么其值可以由phpar传入。

d。 如果是存储过程。

1。执行 call procere()语句。

也就是mysql_query("call proceer([var1]...)");

2. 如果有返回值,执行select @ar,返回执行结果。

mysql_query("select @var)"

接下来的操作就和php执行一般的mysql语句一样了。可以通过mydql_fetch_row()等函数获得结果。

如果时函数。 直接执行 select function() 就可以了。
$host="localhost";
$user="root";
$password="11212";
$db="samp_db";
$dblink=mysql_connect($host,$user,$password)
or die("can't connect to mysql");
mysql_select_db($db,$dblink)
or die("can't select samp_db");
$res=mysql_query("set @a=$password",$dblink);
$res=mysql_query("call aa(@a)",$dblink);
$res=mysql_query("select @a",$dblink);
$row=mysql_fetch_row($res);
echo $row[0];

㈣ mysql中的存储过程是什么意思啊

直白的讲就是把SQL语句进行封装,然后留个接口,使用的时候直接调用接口。

㈤ mysql中的存储过程怎么使用

存储过程(Stored
Procere)是一组为了完成特定功能的SQL语句集功能是将常用或复杂的工作,预先用SQL语句写好并用一个指定名称存储起来,
以后需要数据库提供与已定义好的存储过程的功能相同的服务时,只需调用
call
存储过程名字,
即可自动完成命令。存储过程是由流控制和SQL语句书写的过程,这个过程经编译和优化后存储在数据库服务器中,可由应用程序通过一个调用来执行,而且允许用户声明变量
。同时,存储过程可以接收和输出参数、返回执行存储过程的状态值,也可以嵌套调用。

㈥ MySql编写一个存储过程

1、delimiter // ,声明分隔符:DELIMITER是分割符的意思,因为MySQL默认以";"为分隔符,如果我们没有声明分割符,那么编译器会把存储过程当成SQL语句进行处理,则存储过程的编译过程会报错,所以要事先用DELIMITER关键字申明当前段分隔符,这样MySQL才会将";"当做存储过程中的代码,不会执行这些代码(这里如果不懂的话,你可以通过试错的方法来理解)。
2、编写存储过程的格式:CREATE PROCEDURE([[IN |OUT |INOUT ]参数名数据类形...])
例子:
1)create procere proc1(out s int) // 只有输出
2)create procere proc2(in p_in bigint) // 只有输入
3)create procere proc15() // 没有输入与输出
4)create procere demo_multi_param(in id bigint,in name varchar(32),out c int) //多输入与输出
3、过程体的开始与结束使用BEGIN与END进行标识。
4、select count (*) into s from student; // 过程体,一系列的逻辑语句,sql语句
5、delimiter ; 用完了之后要把分隔符还原。

㈦ mysql存储过程的基本用法有哪些

mysql存储过程的基本用法有哪些
在外部程序访问数据库时(例如 PHP),要组织很多 SQL 语句。

特别是业务逻辑复杂的时候,一大堆的 SQL 和条件夹杂在 PHP 代码中,让人不寒而栗。现在有了 MySQL 存储过程,业务逻辑可以封装存储过程中,这样不仅容易维护,而且执行效率也高。

一、MySQL 创建存储过程

"pr_add" 是个简单的 MySQL 存储过程,这个MySQL 存储过程有两个 int 类型的输入参数 "a"、"b",返回这两个参数的和。
复制代码 代码如下:
drop procere if exists pr_add;

计算两个数之和
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
begin
declare c int;
if a is null then
set a = 0;
end if;
if b is null then
set b = 0;
end if;
set c = a + b;
select c as sum;
/*
return c;
不能在 MySQL 存储过程中使用。return 只能出现在函数中。

*/
end;

二、调用 MySQL 存储过程
复制代码 代码如下:
call pr_add(10, 20);

执行 MySQL 存储过程,存储过程参数为 MySQL 用户变量。
复制代码 代码如下:
set @a = 10;
set @b = 20;
call pr_add(@a, @b);

三、MySQL 存储过程特点

创建 MySQL 存储过程的简单语法为:
复制代码 代码如下:
create procere 存储过程名字()
(
[in|out|inout] 参数 datatype
)
begin
MySQL 语句;
end;

MySQL 存储过程参数如果不显式指定"in"、"out"、"inout",则默认为"in"。习惯上,对于是"in" 的参数,我们都不会显式指定。

1. MySQL 存储过程名字后面的"()"是必须的,即使没有一个参数,也需要"()"

2. MySQL 存储过程参数,不能在参数名称前加"@",如:"@a int"。下面的创建存储过程语法在 MySQL 中是错误的(在 SQL Server 中是正确的)。 MySQL 存储过程中的变量,不需要在变量名字前加"@",虽然 MySQL 客户端用户变量要加个"@"。
复制代码 代码如下:
create procere pr_add
(
@a int, -- 错误
b int -- 正确
)

3. MySQL 存储过程的参数不能指定默认值。

4. MySQL 存储过程不需要在 procere body 前面加 "as"。而 SQL Server 存储过程必须加 "as" 关键字。
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
as -- 错误,MySQL 不需要 "as"
begin
mysql statement ...;
end;

5. 如果 MySQL 存储过程中包含多条 MySQL 语句,则需要 begin end 关键字。
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
begin
mysql statement 1 ...;
mysql statement 2 ...;
end;

6. MySQL 存储过程中的每条语句的末尾,都要加上分号 ";"
复制代码 代码如下:
...
declare c int;
if a is null then
set a = 0;
end if;
...
end;

7. MySQL 存储过程中的注释。
复制代码 代码如下:
/*
这是个
多行 MySQL 注释。
*/
declare c int; -- 这是单行 MySQL 注释 (注意 -- 后至少要有一个空格)
if a is null then # 这也是个单行 MySQL 注释
set a = 0;
end if;

㈧ mysql 存储过程

1. exec 存储过程名
2. exec 存储过程名 参数1,参数2,参数3......
或者exec 存储过程名 参数1='',参数2='',参数3=''......

㈨ mysql存储过程

DELIMITER //
CREATE DEFINER=`root`@`%` PROCEDURE `test_ct_201408`()
BEGIN
set @today=DATE_FORMAT(date(now()),'%Y-%m-%d 00:00:00');
set @create_virus_sql=concat("select * from S_ACCOUNT where date_add(registerTime, interval 6 month)<=NOW() INTO OUTFILE "c:\\",@today,".txt" fields terminated by ','; ");

PREPARE create_virus FROM @create_virus_sql;
EXECUTE create_virus;
commit;
END
//
DELIMITER ;

热点内容
c语言的软件是什么 发布:2025-05-14 09:09:13 浏览:799
php微信支付教程视频教程 发布:2025-05-14 08:59:59 浏览:202
存储服务器分类 发布:2025-05-14 08:39:01 浏览:646
xz文件解压软件 发布:2025-05-14 08:28:43 浏览:970
lua脚本学习 发布:2025-05-14 08:20:55 浏览:714
python文件删除一行 发布:2025-05-14 08:06:58 浏览:722
如何下载奥特曼高级化3安卓版 发布:2025-05-14 07:47:31 浏览:346
qml文件修改后编译未生效 发布:2025-05-14 07:31:00 浏览:331
内到内算法 发布:2025-05-14 07:29:11 浏览:34
文件夹名字不显示 发布:2025-05-14 07:27:47 浏览:775