Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE IF NOT EXISTS `user_groups` (
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
- `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
- `code` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
- `description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
- `created_at` timestamp NULL DEFAULT NULL,
- `updated_at` timestamp NULL DEFAULT NULL,
- PRIMARY KEY (`id`),
- KEY `user_groups_code_index` (`code`)
- ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
- DELETE FROM `user_groups`;
- INSERT INTO `user_groups` (`id`, `name`, `code`, `description`, `created_at`, `updated_at`) VALUES
- (1, 'Guest', 'guest', 'Default group for guest users.', '2017-11-14 03:33:04', '2017-11-14 03:33:04'),
- (2, 'Registered', 'registered', 'Default group for registered users.', '2017-11-14 03:33:04', '2017-11-14 03:33:04'),
- (3, 'Auditores', 'auditores', 'Son los usuarios habilitados a Autorizar las publicaciones de los usuarios.', '2017-11-17 22:18:02', '2017-11-17 22:18:15'),
- (4, 'Estandar', 'estandar', 'Es el grupo de los usuarios estándares.', '2017-11-17 23:04:05', '2017-11-17 23:04:05');
- CREATE DEFINER=`root`@`localhost` PROCEDURE `demoJson`()
- LANGUAGE SQL
- NOT DETERMINISTIC
- CONTAINS SQL
- SQL SECURITY DEFINER
- COMMENT 'AS'
- BEGIN
- DECLARE myVariableJson VARCHAR(100);
- DECLARE done INT DEFAULT FALSE;
- DECLARE oneRecord VARCHAR(200);
- DECLARE allRecord VARCHAR(600);
- DECLARE nIndex INT(3);
- DECLARE cur1 CURSOR FOR SELECT JSON_OBJECT("pkey",id, "nombre",name, "codigo",code) AS campito1
- FROM user_groups;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
- OPEN cur1;
- SET nIndex:=0;
- read_loop: LOOP
- FETCH cur1 INTO oneRecord;
- IF done THEN
- LEAVE read_loop;
- END IF;
- SET nIndex:=nIndex+1;
- IF nIndex=1 THEN
- SET allRecord := oneRecord;
- ELSE
- SELECT JSON_MERGE(allRecord, oneRecord) INTO allRecord;
- END IF;
- END LOOP;
- SET myVariableJson := JSON_OBJECT('variable1', 'value1','variable2','value2');
- SELECT JSON_MERGE(myVariableJson, allRecord) ;
- CLOSE cur1;
- END
- CALL `demoJson`();
- ---- resultado ----
- {"pkey": [1, 2, 3, 4], "codigo": ["guest", "registered", "auditores", "estandar"], "nombre": ["Guest", "Registered", "Auditores", "Estandar"], "variable1": "value1", "variable2": "value2"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement