1, a simple stored procedure
defined:
drop Procedure IF EXISTS f_mis;
Create PROCEDURE f_mis (the IN p_masterId int, the IN p_serverId int, the IN p_attributeId int, OUT p_mapKey VARCHAR (256), OUT p_attributeValue VARCHAR (256))
the BEGIN
the SELECT CONCAT (MasterId, serverId, the attributeId), attributeValue p_mapKey` INTO `,` p_attributeValue`
the FROM table_xxx
WHERE MasterId = serverId = `` p_masterId` and p_serverId` and the attributeId = `p_attributeId` and instantId = '1800';
the END
NOTE: varchar (256) type must specify the length, otherwise an error
Calls:
Call f_mis (65,64,72, p_mapkey @, @ p_value);
SELECT p_mapkey @, @ p_value;
NOTE: In reference to an int 65,64,72, not single quotes
2, the combined column values stored procedure
drop Procedure IF EXISTS f_append_value;
Create PROCEDURE f_append_value ()
the BEGIN
- field values at cursor loop for receiving
the DECLARE i_instant_id int;
the DECLARE a_attribute_value VARCHAR (1000);
-- 声明游标
DECLARE append_value CURSOR for SELECT t.instantId, t.attributeValue FROM table_xxx t;
- Renounce cycling conditions: When a record is not found, exit the cursor
DECLARE EXIT HANDLER FOR NOT FOUND CLOSE append_value ;
- The return value, procedure or operation result stored
SET @result = '';
-- open 光标
OPEN append_value;
-- 循环拼接字符串
REPEAT
FETCH append_value INTO i_instant_id, a_attribute_value;
IF i_instant_id = 1800 THEN
SET @result = CONCAT(a_attribute_value,'---',@result);
END IF;
UNTIL 0 END REPEAT;
-- close 光标
CLOSE append_value;
END
调用:
call f_append_value();
select @result;
Stored Procedures:
https://www.cnblogs.com/huaxingtianxia/p/5628828.html
Reports
stored procedures cursor
https://blog.csdn.net/datouniao1/article/details/81903738
https://blog.csdn.net/m0_37897502/article/details/80911727