I have a mySQL stored procedure that work with few data in a table :
SQL Fiddle Demo -> thanks to Mahmoud Gamal for this.
The problem is that this precedure doesn't work with many metadata :
CREATE TABLE Metadata (`metadata_id` int, `metadata_name` varchar(25)) ;
INSERT INTO Metadata
(`metadata_id`, `metadata_name`)
VALUES
(1, 'duration'),
(2, 'extension'),
(3, 'size'),
(4, 'duration2'),
(5, 'extension2'),
(6, 'size2'),
(7, 'duration3'),
(8, 'extension3'),
(9, 'size3'),
(10, 'duration4'),
(11, 'extension4'),
(12, 'size4'),
(13, 'duration5'),
(14, 'extension5'),
(15, 'size5'),
(16, 'duration6'),
(17, 'extension6'),
(18, 'size6')
;
It seems that sessions variables declared with @ are too short !
And this table will have around 150 metadata at the end.
SET @sql = NULL;
SET @cols = NULL;
SELECT
GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(m.metadata_name = ''',
m.metadata_name, ''', v.value, 0)) AS ', '''', m.metadata_name, '''')
) INTO @cols
FROM Metadata AS m;
SET @sql = CONCAT('
SELECT
v.file, ', @cols ,'
FROM `values` AS v
INNER JOIN metadata AS m ON v.metadata_id = m.metadata_id
GROUP BY v.file');
prepare stmt
FROM @sql;
execute stmt;
The two variables @sql
and @cols
work fine for simple scenarios like in this demo, but in my case I have 150 metadata, in this case I got an error that the variable is too short.
How to declare the two variables so that they take this too long text coming from concatenating 150 metadata names?
Thanks in advance