如何在mysql存储过程中声明变量?

时间:2015-06-23 07:06:51

标签: mysql sql stored-procedures

我想声明一个变量,然后将select查询的值赋给该特定变量,然后我需要在insert语句中传递该变量。所以我尝试了下面的代码,

DROP PROCEDURE IF EXISTS `pro_damagestockdao` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `pro_damagestockdao`(
IN damage_date date,
IN damage_inv_no varchar(12),
IN damage_inv_date date,
IN damage_dist_name varchar(30),
IN damage_contact_no varchar(15),
IN damage_item_code varchar(30),
IN damage_item_name varchar(30),
IN damage_batch_no varchar(15),
IN damage_mfr_name varchar(50),
IN damage_expiry_date date,
IN damage_pur_qty int(11),
IN damage_qty int(11),
IN damage_unit_price double(10,2),
IN damage_unit_vat double(4,2),
IN damage_unit_discount double(4,2),
IN damage_sub_total double(10,2),
IN damage_total_amount double(10,2),
IN damage_remarks varchar(1000),
IN functionality varchar(20),
out flag int
)
BEGIN

DECLARE selCnt int;
DECLARE dqty int;
SET dqty = (SELECT free FROM purchase_invoice p WHERE p.invoice_no  = damage_inv_no);

DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;

SET flag=0;

START TRANSACTION;

if(functionality='save') then

INSERT INTO damage_stocks
(damage_stock_date,invoice_no,invoice_date,dist_name,contact_no,item_code,item_name,batch_no,qty,damaged_qty,unit_price,unit_vat,unit_discount,sub_total,total_amount,remarks) VALUES
(damage_date,damage_inv_no,damage_inv_date,damage_dist_name,damage_contact_no,0,damage_item_name,damage_batch_no,dqty,damage_qty,damage_unit_price,damage_unit_vat,damage_unit_discount,damage_sub_total,damage_total_amount,damage_remarks);

但是它失败了..你看,我声明了一个变量dqty,然后我把它传递给insert语句。

2 个答案:

答案 0 :(得分:0)

声明不是问题。这是分配问题 试试这个查询

SELECT free into dqty  FROM purchase_invoice p WHERE p.invoice_no  = damage_inv_no;

答案 1 :(得分:0)

以下代码适用于我:

DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `pro_damagestockdao`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `pro_damagestockdao`(
IN damage_date DATE,
IN damage_inv_no VARCHAR(12),
IN damage_inv_date DATE,
IN damage_dist_name VARCHAR(30),
IN damage_contact_no VARCHAR(15),
IN damage_item_code VARCHAR(30),
IN damage_item_name VARCHAR(30),
IN damage_batch_no VARCHAR(15),
IN damage_mfr_name VARCHAR(50),
IN damage_expiry_date DATE,
IN damage_pur_qty INT(11),
IN damage_qty INT(11),
IN damage_unit_price DOUBLE(10,2),
IN damage_unit_vat DOUBLE(4,2),
IN damage_unit_discount DOUBLE(4,2),
IN damage_sub_total DOUBLE(10,2),
IN damage_total_amount DOUBLE(10,2),
IN damage_remarks VARCHAR(1000),
IN functionality VARCHAR(20),
OUT flag INT)
BEGIN
DECLARE selCnt INT;
DECLARE dqty INT;   
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;

SET dqty = (SELECT free FROM purchase_invoice p WHERE p.invoice_no  = damage_inv_no);
SET flag = 0;
START TRANSACTION;
IF(functionality = 'save') THEN
    INSERT INTO damage_stocks (damage_stock_date, invoice_no, invoice_date, dist_name, contact_no, item_code, item_name, batch_no, qty,damaged_qty, unit_price, unit_vat, unit_discount, sub_total, total_amount, remarks) 
    VALUES (damage_date, damage_inv_no, damage_inv_date, damage_dist_name, damage_contact_no, 0, damage_item_name, damage_batch_no, dqty,damage_qty, damage_unit_price, damage_unit_vat, damage_unit_discount, damage_sub_total, damage_total_amount, damage_remarks);

END IF; 

END$$

DELIMITER ;

通过此SP呼叫:

CALL `pro_damagestockdao`("2015-06-23","rx45","2015-06-20","DistName","DContactNo","DamageItemCode","damage_item_name","damage_batch_no","damage_mfr_name", "2015-07-01", 10, 5, 15.2, 7.66, 9.88, 99.00, 150.22, "No remarks", "save", @sorin);

你得到什么错误? 尝试创建存储过程或调用存储过程时是否出错?