DELIMITER $$
 DROP PROCEDURE IF EXISTS duplicateDelegation$$
 CREATE PROCEDURE duplicateDelegation(delegationId VARCHAR(255), i INT)
       BEGIN              
               DECLARE x INT;
               DECLARE delegId VARCHAR(255);
               DECLARE delegDN VARCHAR(255);
               DECLARE delegFQAN VARCHAR(255);
               DECLARE delegVO VARCHAR(50);
               DECLARE delegLocalUser VARCHAR(100);
               DECLARE delegLocalUserGroup VARCHAR(100);
               DECLARE delegVomsAttribute text;
               DECLARE delegCertificate text;
               DECLARE delegInfo text;
               DECLARE delegStartTime datetime;
               DECLARE delegExpirationTime datetime;
               DECLARE delegLastUpdateTime datetime;

               SET x = 1;
               select id, dn, fqan, vo, localUser, localUserGroup, vomsAttribute, certificate, info, startTime, expirationTime, lastUpdateTime INTO delegId, delegDN, delegFQAN, delegVO, delegLocalUser, delegLocalUserGroup, delegVomsAttribute, delegCertificate, delegInfo, delegStartTime, delegExpirationTime, delegLastUpdateTime FROM delegation where id=delegationId;

               WHILE x <= i DO
                  insert into delegation (id, dn, fqan, vo, localUser, localUserGroup, vomsAttribute, certificate, info, startTime, expirationTime, lastUpdateTime) VALUES (CONCAT(delegId,'_',x), delegDN, delegFQAN, delegVO, delegLocalUser, delegLocalUserGroup, delegVomsAttribute, delegCertificate, delegInfo, delegStartTime, delegExpirationTime, delegLastUpdateTime);
                  commit;
                  SET  x = x + 1; 
               END WHILE;
       END$$
   DELIMITER ;
