I am creating a SP and when giving parameter as follow CALL sp_lookupvalue_count('location',NULL,50). When giving 'location' parameter value and defining cursor on it but amazingly it is not returning any records.
But if run simple select statement of cursor then its returning value. Sharing my SP text. Please guide.
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_lookupvalue_count`(IN p_etype VARCHAR(100),IN p_type_id INT,IN p_record_id INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE v_tab VARCHAR(2000);
-- Define relevant cursors
DECLARE curs1 CURSOR FOR
SELECT CONCAT('insert into temp_table_count(total_rows,lookup_type,table_name) select ifnull(count(*),null),',IFNULL(er.lookup_type_id,0),',''',er.table ,''' from ',er.table,' where ',er.column,' = ',p_record_id) tab
FROM entity_references er
WHERE IF(p_type_id IS NULL,er.entity_type = p_etype, er.lookup_type_id = p_type_id);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
--
-- Tempporary table
DROP TEMPORARY TABLE IF EXISTS temp_table_count;
CREATE TEMPORARY TABLE temp_table_count(lookup_type INT,table_name VARCHAR(200),total_rows INT);
OPEN curs1;
FETCH curs1 INTO v_tab;
read_loop: LOOP
FETCH curs1 INTO v_tab;
IF done THEN
CLOSE curs1;
LEAVE read_loop;
END IF;
SET @tab := v_tab;
PREPARE stmt1 FROM @tab;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END LOOP;
SELECT * FROM temp_table_count WHERE total_rows0;
END$$
DELIMITER ;
Aucun commentaire:
Enregistrer un commentaire