Created
September 18, 2012 19:24
-
-
Save avoidwork/3745240 to your computer and use it in GitHub Desktop.
Stored procedure which generates a comma delimited string of UUIDs of related records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN | |
DECLARE `uuid` CHAR(50); | |
DECLARE parent CHAR(255); | |
DECLARE singular CHAR(255); | |
DECLARE source CHAR(255); | |
SET `uuid` = (SELECT udf_uuid(guid)); | |
SET parent = (SELECT udf_type(guid)); | |
SET singular = (SELECT udf_singular(type)); | |
SET source = (SELECT CONCAT(type, udf_capitalize(parent), 's')); | |
SET @uuids = ''; | |
SET @stmt = CONCAT("SELECT GROUP_CONCAT(`", singular, "`, ',') INTO @uuids FROM ", source, " WHERE `", parent, "` = '", `uuid`, "'"); | |
PREPARE tmp FROM @stmt; | |
EXECUTE tmp; | |
DEALLOCATE PREPARE tmp; | |
SET @uuids = (SELECT SUBSTRING(@uuids, 1, LENGTH(@uuids) - 1)); | |
SELECT @uuids AS `results`; | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment