mysql 的預存程序_多欄位

來源:互聯網
上載者:User

標籤:utf8   proc   ext   sql   ali   for   ble   mil   roc   

mysql 的預存程序

 

      一、準備工作

            建立一個表

/*
Navicat MySQL Data Transfer

Source Server : localhost_3306
Source Server Version : 50505
Source Host : localhost:3306
Source Database : kamanage

Target Server Type : MYSQL
Target Server Version : 50505
File Encoding : 65001

Date: 2018-08-31 17:43:23
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `temp_user`
-- ----------------------------
DROP TABLE IF EXISTS `temp_user`;
CREATE TABLE `temp_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of temp_user
-- ----------------------------
INSERT INTO `temp_user` VALUES (‘4‘, ‘zhanghao‘, ‘2‘);
INSERT INTO `temp_user` VALUES (‘5‘, ‘sql‘, ‘30‘);
INSERT INTO `temp_user` VALUES (‘6‘, ‘pengpeng‘, ‘30‘);

 

二、預存程序使用的範例:尋找到該表裡的所有資料,並刪除

################################適用於多欄位###############################

create PROCEDURE  delete_data()

begin

  declare name1 char(30);     

  declare age1 int (3);

  declare end_tag int default 0;#迴圈結束標誌  預設是0,代表0的時候都可以迴圈

 declare list CURSOR for  select name,age from  temp_user;

 declare  CONTINUE  HANDLER  for  not FOUND  set end_tag=1  ;#為  未找到集合 生命處理常式  ,把迴圈結束標誌設定為1

 open list; ###開啟遊標

repeat ###迴圈開始

FETCH list into name1,age1;#遊標開始一個一個滑動

 

IF not end_tag then  ###  if not    !=1 結束符!=1  就刪除

                  

delete from temp_user  where name=name1 and age=age1;

      

end if;

 

UNTIL  end_tag end repeat;#直到 end_tag=1(迴圈結束符值為1的時候)結束迴圈

 

 

end ;

call delete_data();####調用預存程序

 

drop procedure delete_data;####刪除預存程序

 

mysql 的預存程序_多欄位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.