mysql存储过程
创建存储过程
创建例子一:
DELIMITER $$
CREATE PROCEDURE query_user( currCID INT )
BEGIN
DECLARE currKeywordID INT;
DECLARE currTitle VARCHAR(255);
DECLARE currKeyword VARCHAR(255);
DECLARE currContent TEXT;
DECLARE keywordsCSV VARCHAR(255);
SELECT NOW();
END;
创建例子二:
/*======================
打开数据库
========================*/
USE wechatPlugin;
/*======================
删除已经存在的存储过程
========================*/
DROP PROCEDURE IF EXISTS query_companyPlugins;
/*
开始声明存储过程
*/
DELIMITER $$
CREATE PROCEDURE query_companyPlugins( alias VARCHAR(32),companyName VARCHAR(32) )
BEGIN
DECLARE currKeywordID INT;
DECLARE currTitle VARCHAR(255);
DECLARE currKeyword VARCHAR(255);
DECLARE currContent TEXT;
DECLARE keywordsCSV VARCHAR(255);
SELECT * FROM plugin_companyPluginsa,PLUGIN b
WHERE a.pluginName = b.pluginName
#and a.companyName='palmv';
AND a.companyName=companyName;
END;
用法3:
/*======================
打开数据库
========================*/
USE wechatPlugin;
/*======================
删除已经存在的存储过程
========================*/
DROP PROCEDURE IF EXISTS query_companyPlugins;
/*
开始声明存储过程
*/
DELIMITER $$
CREATE PROCEDURE query_companyPlugins(
optType INT, # 1:增加 2:修改(仅能修改buyClientCount) 3:删除 4;查询
var_companyName VARCHAR(32), #查询、删除必须
var_pluginName VARCHAR(32), #删除时:为空表示删除该公司所有的插件
var_buyClientCount INT
)
BEGIN
DECLARE currKeywordID INT; #暂时没有任何用途
IF optType = 4 THEN #查询操作
SELECT * FROM plugin_companyPlugins a
WHERE a.companyName=companyName;
ELSEIF optType = 3 THEN #删除操作
IF ISNULL( pluginName ) || LENGTH(TRIM(pluginName))<1 THEN
DELETE FROM plugin_companyplugins WHERE companyName = var_companyName; #'palmv' ;
ELSE
DELETE FROM plugin_companyplugins WHERE companyName = var_companyName AND pluginName=var_pluginName ;
END IF;
ELSEIF optType = 2 THEN #修改操作
UPDATE plugin_companyplugins SET buyClientCount = var_buyClientCount WHERE companyName = var_companyName AND pluginName = var_pluginName ;
ELSEIF optType = 1 THEN #增加操作
INSERT INTO plugin_companyPlugins (companyName, pluginName, buyClientCount)
VALUES (var_companyName,var_pluginName, var_buyClientCount);
ELSE
SELECT '错误操作类型参数';
END IF ;
END;
===============执行存储过程=========
/*=============
测试执行存储过程
特别说明不能直接接在上面,具体原因暂时不清楚
*/
/*======================
调用用例4——1 查询公司可以用的plugins
========================*/
USE wechatPlugin;
SET @optType = 4;
SET @companyName = 'palmv';
SET @pluginName = NULL;
SET @buyClientCount = NULL;
CALL opt_companyPlugins (
@optType , @companyName, @pluginName, @buyClientCount
);
/*======================
调用用例4——2 查询公司指定的plugins
========================*/
USE wechatPlugin;
SET @optType = 4;
SET @companyName = 'palmv';
SET @pluginName = 'Sns2Sns';
SET @buyClientCount = NULL;
CALL opt_companyPlugins (
@optType , @companyName, @pluginName, @buyClientCount
);
调用用例4——1 查询公司可以用的plugins
========================*/
USE wechatPlugin;
SET @optType = 4;
SET @companyName = 'palmv';
SET @pluginName = NULL;
SET @buyClientCount = NULL;
CALL opt_companyPlugins (
@optType , @companyName, @pluginName, @buyClientCount
);
/*======================
调用用例4——2 查询公司指定的plugins
========================*/
USE wechatPlugin;
SET @optType = 4;
SET @companyName = 'palmv';
SET @pluginName = 'Sns2Sns';
SET @buyClientCount = NULL;
CALL opt_companyPlugins (
@optType , @companyName, @pluginName, @buyClientCount
);