I need to write Mysql 8 procedure which make union of data from several tables and return some result set returned :
I need some tree structure like :
UserPermisionTickets =>
UserName =>
TicketUser;
I try to make it in a procedure with 2 cursors, but I do not know 1) How to use parameter for the 2nd cursor ?
DELIMITER $$
DROP PROCEDURE IF EXISTS getUserPermissionTickets $$
create PROCEDURE getUserPermissionTickets(IN permission_id INT, IN user_id INT)
BEGIN
DECLARE userId INT;
DECLARE userName VARCHAR(255);
DECLARE permissionName VARCHAR(255);
DECLARE userCreatedAt DATETIME;
DECLARE ticketId INT;
DECLARE teamLead tinyint(1);
DECLARE percent tinyint;
DEClARE cursorUserPermission
CURSOR FOR
SELECT users.id as user_id, users.name as user_name, spt_permissions.name as permission_name, users.created_at as user_created_at
FROM spt_model_has_roles
join users, spt_permissions
where spt_permissions.id = spt_model_has_roles.role_id and
users.id = spt_model_has_roles.model_id ;
DECLARE cursorTicketUser CURSOR FOR SELECT ticket_id, team_lead, percent FROM ticket_user where user_id = :user_id;
-- 1) Need to get parameters from External loop cursorUserPermission ?
-- External loop
OPEN cursorUserPermission;
userPermissionLoop: LOOP
FETCH cursorUserPermission INTO userId, userName, permissionName, userCreatedAt;
-- Internal loop
OPEN cursorTicketUser;
TicketUserLoop: LOOP
FETCH cursorTicketUser INTO ticketId, teamLead, percent;
END LOOP TicketUserLoop;
CLOSE cursorTicketUser;
END LOOP userPermissionLoop;
CLOSE cursorUserPermission;
END $$
I need to return data which would be data from 1st cursor extended with data from 2nd cursor ?
How can I do it ?
When I try to compile I got error :
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':user_id;
-- Need to get parameters from External loop cursorUserPermission ' at line 20
Thanks in advance!