Skip to content

Instantly share code, notes, and snippets.

@avoidwork
Created September 18, 2012 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save avoidwork/3745240 to your computer and use it in GitHub Desktop.
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
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