Mysql 游标示例

Mysql 游标示例,第1张

CREATE  PROCEDURE `gk_test`()

COMMENT '测试数据'  -- 函数注释

BEGIN

    DECLARE done INT DEFAULT FALSE -- 定义遍历数据结束标志。非游标变量,需要定义在游标变量

    DECLARE m_id CHAR(32) -- 定义接收游标数据中id变量

    DECLARE m_or VARCHAR(255) -- 定义接收游标数据中orderno变量

    DECLARE m_c INT(11)  -- 定义接收游标数据中count变量

    DECLARE m_soult CURSOR FOR SELECT `id`, `orderno`, count(*) t FROM hb_shipper_order GROUP BY orderno HAVING t >1   -- 定义游标变量,即接收查询结果集

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE  -- 将结束标志绑定到游标

    OPEN m_soult    -- 打开游标

    read_loop:LOOP   -- 开始循环。这个是mysql 3种循环中的1种。

        FETCH m_soult INTO m_id,m_or,m_c   -- 提取游标数据

        IF done THEN   -- 判断,当为true时

            LEAVE read_loop   -- 跳出循环

        END IF

        -- 处理事 自己想处理的事

        UPDATE `hb_shipper_waybill` SET

        ordernos = REPLACE (`ordernos`, m_or, CONCAT(m_or,'-2')),

        goodsname = REPLACE (`goodsname`, m_or, CONCAT(m_or,'-2'))

        WHERE `id` IN ( SELECT `waybillid` FROM `hb_shipper_order_waybill_relation` WHERE orderid = m_id)

        UPDATE `hb_shipper_order` SET orderno = CONCAT(m_or,'-2') WHERE id = m_id

    END LOOP  -- 结束循环

    -- 关闭游标

    CLOSE m_soult

END

上图:

DEMO: 2    // 通过 存储过程,处理  copy 数据并处理对应数据关系

delimiter $$

DROP PROCEDURE IF EXISTS gk

CREATE DEFINER =  PROCEDURE `gk`(IN pname varchar(255))

    COMMENT '测试'

    BEGIN

        DECLARE new_id CHAR(32)

        DECLARE m_id CHAR(32)

        DECLARE new_noids VARCHAR(255)

        DECLARE done INT DEFAULT 0

        DECLARE m_result CURSOR FOR select id from `xxx`

        DECLARE m_result2 CURSOR FOR select id from `xxx`

        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1

        DROP TABLE IF EXISTS `xxx`

        CREATE TABLE `xxx` (

                `id` char(32) NOT NULL COMMENT 'ID',

                `subsystem` char(32) NOT NULL COMMENT '',

                `name` varchar(20) NOT NULL COMMENT '',

                `type` varchar(15) NOT NULL COMMENT '',

                `url` varchar(200) DEFAULT NULL COMMENT '',

                `icon` varchar(20) DEFAULT NULL COMMENT '',

                `target` varchar(10) NOT NULL DEFAULT '_self' COMMENT '',

                `method` mediumtext COMMENT '',

                `orderby` smallint(3) NOT NULL DEFAULT '0' COMMENT '',

                `parentid` char(32) NOT NULL DEFAULT '0' COMMENT '',

                `relateid` char(32) NOT NULL COMMENT '',

                `isopen` tinyint(1) NOT NULL DEFAULT '1' COMMENT '',

                `nodeids` varchar(255) DEFAULT NULL COMMENT '',

                `nodenum` smallint(3) NOT NULL DEFAULT '0' COMMENT '',

                `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '',

                `createuser` char(32) NOT NULL DEFAULT '0' COMMENT '',

                `updatetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '',

                `udateuser` char(32) NOT NULL DEFAULT '0' COMMENT '',

                `deleted` tinyint(1) DEFAULT '0' COMMENT '',

                `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '',

                `isdefault` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '',

                `isdisable` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '',

                `projecttype` varchar(255) NOT NULL COMMENT '',

            PRIMARY KEY (`id`)

        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT

        INSERT INTO `xxxx` SELECT * FROM `aaa` WHERE `projecttype` = 'T'

        UPDATE `xxx` SET `projecttype` = pname

        OPEN m_result

        REPEAT

                FETCH m_result INTO m_id

                        SET new_id = UPPER(REPLACE(UUID(),'-',''))

                        UPDATE `xxx` SET `id` = new_id WHERE `id` = m_id

                        UPDATE `xxx` SET `parentid` = new_id WHERE `parentid` = m_id

        UNTIL done END REPEAT

        CLOSE m_result

        SET done = 0

        OPEN m_result2

        REPEAT

                FETCH m_result2 INTO m_id

                        SELECT CONCAT(',', GROUP_CONCAT(`id`)) INTO new_noids FROM `xxx` WHERE `parentid` = m_id

                        UPDATE `xxx` SET `nodeids` = new_noids WHERE `id` = m_id

        UNTIL done END REPEAT    

        CLOSE m_result2

        INSERT INTO `aaa` SELECT * FROM `xxx`

        DROP TABLE IF EXISTS xxx

    END $$

delimiter

1

用mysql客户端登入

2

选择数据库

mysql>use

test

3

查询当前数据库有哪些存储过程

mysql>show

procedure

status

where

db='test'

4

创建一个简单的存储过程

mysql>create

procedure

hi()

select

'hello'

5

存储过程创建完毕,看怎么调用它

mysql>call

hi()

显示结果

mysql>

call

hi()

+-------+

|

hello

|

+-------+

|

hello

|

+-------+

1

row

in

set

(0.00

sec)

query

ok,

0

rows

affected

(0.01

sec)

6

一个简单的储存过程就成功了

这篇文章主要介绍了PHP同时连接多个mysql数据库的具体实现,需要的朋友可以参考下

实例:

代码如下:

<?php

$conn1

=

mysql_connect("127.0.0.1",

"root","root","db1")

mysql_select_db("db1",

$conn1)

$conn2

=

mysql_connect("127.0.0.1",

"root","root","db2")

mysql_select_db("db2",

$conn2)

$sql

=

"select

*

from

ip"

$query

=

mysql_query($sql)

if($row

=

mysql_fetch_array($query))

echo

$row[0]."n"

$sql

=

"select

*

from

web

"

$query

=

mysql_query($sql)

if($row

=

mysql_fetch_array($query))

echo

$row[0]

?>

这段代码存在问题,在程序执行时会报错:PHP

Warning:

mysql_fetch_array()

expects

parameter

1

to

be

resource,

boolean

given

in

....

原因分析:

程序开始建立两个数据库链接,函数mysql_query()原型:

resource

mysql_query

(

string

$query

[,

resource

$link_identifier

]

)

向与指定的连接标识符关联的服务器中的当前活动数据库发送一条查询。如果没有指定

link_identifier,则使用上一个打开的连接。如果没有打开的连接,本函数会尝试无参数调用

mysql_connect()

函数来建立一个连接并使用之。查询结果会被缓存。

在本例中由于没有指定link_identifier,所以,在执行第一条sql时,默认使用的是上一个打开的链接,即$conn2,而实际上第一条sql语句应该使用的是$conn1,所以导致报错,所以为了能够链接多个mysql数据库,可以使用如下方法:

方法1:在mysql_query函数中指定所用连接,即:

代码如下:

<?php

$conn1

=

mysql_connect("127.0.0.1",

"root","root","db1")

mysql_select_db("Muma",

$conn1)

$conn2

=

mysql_connect("127.0.0.1",

"root","root","db2")

mysql_select_db("product",

$conn2)

$sql

=

"select

*

from

ip"

$query

=

mysql_query($sql,$conn1)

//添加连接$conn1

if($row

=

mysql_fetch_array($query))

echo

$row[0]."n"

$sql

=

"select

*

from

web

"

$query

=

mysql_query($sql,

$conn2)

if($row

=

mysql_fetch_array($query))

echo

$row[0]

?>

方法2:在sql语句中关联所用数据库,此时可以省略mysql_query的第二个参数,即:

代码如下:

<?php

$conn1

=

mysql_connect("127.0.0.1",

"root","root","db1")

mysql_select_db("db1",

$conn1)

$conn2

=

mysql_connect("127.0.0.1",

"root","root","db2")

mysql_select_db("db2",

$conn2)

$sql

=

"select

*

from

db1.ip"

//关联数据库

$query

=

mysql_query($sql)

if($row

=

mysql_fetch_array($query))

echo

$row[0]."n"

$sql

=

"select

*

from

db2.web

"

$query

=

mysql_query($sql)

if($row

=

mysql_fetch_array($query))

echo

$row[0]

?>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7489643.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存