SQL procedure - Multiple value











up vote
1
down vote

favorite












I have this procedure :



BEGIN 
DECLARE done INT DEFAULT FALSE;
DECLARE `id_var` varchar(255);
DECLARE `cur1` CURSOR FOR
SELECT `id` FROM `clients`
WHERE `status` = 'Active';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DROP TABLE IF EXISTS `tblquota_nc`;
CREATE TABLE IF NOT EXISTS `tblquota_nc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`email` varchar(255),
`pack_id` int(11) NOT NULL,
`pack_name` varchar(255) NOT NULL,
`quota` int(11) NULL,
PRIMARY KEY (`id`)
);
OPEN cur1;
read_loop: LOOP
FETCH NEXT
FROM cur1
INTO id_var;
IF done THEN
LEAVE read_loop;
END IF;

SELECT clients.id as `User ID`, `email`, `packageid` as `Pack ID`, `name` as `Pack`, (CASE
WHEN `name` = "Basic" THEN '10'
WHEN `name` = "Silver" THEN '100'
WHEN `name` = "Gold" THEN '1000'
ELSE '10'
END) as Quota INTO @mid, @mail, @p_id, @packname, @quota
FROM `clients`
INNER JOIN `tblhosting` ON clients.id = tblhosting.userid
INNER JOIN `tblproducts` ON tblhosting.packageid = tblproducts.id
WHERE clients.status = 'Active'
AND tblhosting.domainstatus = 'Active'
AND clients.id = id_var;
IF (SELECT id FROM tblquota_nc WHERE user_id = @mid) THEN
BEGIN
END;
ELSE
BEGIN
if (@mid IS NOT NULL AND @p_id IS NOT NULL AND @packname IS NOT NULL) then
INSERT INTO tblquota_nc (user_id, email, pack_id, pack_name, quota) VALUES (@mid, @mail, @p_id, @packname, @quota);
end if;
END;
END IF;
END LOOP;
CLOSE cur1;
END


It seems like I have an error because the SELECT statment return several values. I thought doing another loop with these results to make an insert into the new table. I want to make a new table from these information.



table clients:



id | email          | status
----------------------------
1 | user1@mail.com | Active
2 | user2@mail.com | Inactive
3 | user3@mail.com | Active


table tblhosting



id | userid | packageid | domainstatus 
------------------------------------------------
1 | 1 | 2 | Active
2 | 2 | 3 | Active
3 | 3 | 1 | Active


table tblproducts



id | name 
-----------
1 | Basic
2 | Silver
3 | Gold


I expect result like :



id | user_id | email          | pack_id | pack_name | quota
-----------------------------------------------------------
1 | 1 | user1@mail.com | 2 | Silver | 100
2 | 2 | user2@mail.com | 3 | Gold | 1000
3 | 3 | user3@mail.com | 1 | Basic | 10


If I put max in the case statment, it will work but will not show all data.










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have this procedure :



    BEGIN 
    DECLARE done INT DEFAULT FALSE;
    DECLARE `id_var` varchar(255);
    DECLARE `cur1` CURSOR FOR
    SELECT `id` FROM `clients`
    WHERE `status` = 'Active';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    DROP TABLE IF EXISTS `tblquota_nc`;
    CREATE TABLE IF NOT EXISTS `tblquota_nc` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11) NOT NULL,
    `email` varchar(255),
    `pack_id` int(11) NOT NULL,
    `pack_name` varchar(255) NOT NULL,
    `quota` int(11) NULL,
    PRIMARY KEY (`id`)
    );
    OPEN cur1;
    read_loop: LOOP
    FETCH NEXT
    FROM cur1
    INTO id_var;
    IF done THEN
    LEAVE read_loop;
    END IF;

    SELECT clients.id as `User ID`, `email`, `packageid` as `Pack ID`, `name` as `Pack`, (CASE
    WHEN `name` = "Basic" THEN '10'
    WHEN `name` = "Silver" THEN '100'
    WHEN `name` = "Gold" THEN '1000'
    ELSE '10'
    END) as Quota INTO @mid, @mail, @p_id, @packname, @quota
    FROM `clients`
    INNER JOIN `tblhosting` ON clients.id = tblhosting.userid
    INNER JOIN `tblproducts` ON tblhosting.packageid = tblproducts.id
    WHERE clients.status = 'Active'
    AND tblhosting.domainstatus = 'Active'
    AND clients.id = id_var;
    IF (SELECT id FROM tblquota_nc WHERE user_id = @mid) THEN
    BEGIN
    END;
    ELSE
    BEGIN
    if (@mid IS NOT NULL AND @p_id IS NOT NULL AND @packname IS NOT NULL) then
    INSERT INTO tblquota_nc (user_id, email, pack_id, pack_name, quota) VALUES (@mid, @mail, @p_id, @packname, @quota);
    end if;
    END;
    END IF;
    END LOOP;
    CLOSE cur1;
    END


    It seems like I have an error because the SELECT statment return several values. I thought doing another loop with these results to make an insert into the new table. I want to make a new table from these information.



    table clients:



    id | email          | status
    ----------------------------
    1 | user1@mail.com | Active
    2 | user2@mail.com | Inactive
    3 | user3@mail.com | Active


    table tblhosting



    id | userid | packageid | domainstatus 
    ------------------------------------------------
    1 | 1 | 2 | Active
    2 | 2 | 3 | Active
    3 | 3 | 1 | Active


    table tblproducts



    id | name 
    -----------
    1 | Basic
    2 | Silver
    3 | Gold


    I expect result like :



    id | user_id | email          | pack_id | pack_name | quota
    -----------------------------------------------------------
    1 | 1 | user1@mail.com | 2 | Silver | 100
    2 | 2 | user2@mail.com | 3 | Gold | 1000
    3 | 3 | user3@mail.com | 1 | Basic | 10


    If I put max in the case statment, it will work but will not show all data.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have this procedure :



      BEGIN 
      DECLARE done INT DEFAULT FALSE;
      DECLARE `id_var` varchar(255);
      DECLARE `cur1` CURSOR FOR
      SELECT `id` FROM `clients`
      WHERE `status` = 'Active';
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
      DROP TABLE IF EXISTS `tblquota_nc`;
      CREATE TABLE IF NOT EXISTS `tblquota_nc` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `user_id` int(11) NOT NULL,
      `email` varchar(255),
      `pack_id` int(11) NOT NULL,
      `pack_name` varchar(255) NOT NULL,
      `quota` int(11) NULL,
      PRIMARY KEY (`id`)
      );
      OPEN cur1;
      read_loop: LOOP
      FETCH NEXT
      FROM cur1
      INTO id_var;
      IF done THEN
      LEAVE read_loop;
      END IF;

      SELECT clients.id as `User ID`, `email`, `packageid` as `Pack ID`, `name` as `Pack`, (CASE
      WHEN `name` = "Basic" THEN '10'
      WHEN `name` = "Silver" THEN '100'
      WHEN `name` = "Gold" THEN '1000'
      ELSE '10'
      END) as Quota INTO @mid, @mail, @p_id, @packname, @quota
      FROM `clients`
      INNER JOIN `tblhosting` ON clients.id = tblhosting.userid
      INNER JOIN `tblproducts` ON tblhosting.packageid = tblproducts.id
      WHERE clients.status = 'Active'
      AND tblhosting.domainstatus = 'Active'
      AND clients.id = id_var;
      IF (SELECT id FROM tblquota_nc WHERE user_id = @mid) THEN
      BEGIN
      END;
      ELSE
      BEGIN
      if (@mid IS NOT NULL AND @p_id IS NOT NULL AND @packname IS NOT NULL) then
      INSERT INTO tblquota_nc (user_id, email, pack_id, pack_name, quota) VALUES (@mid, @mail, @p_id, @packname, @quota);
      end if;
      END;
      END IF;
      END LOOP;
      CLOSE cur1;
      END


      It seems like I have an error because the SELECT statment return several values. I thought doing another loop with these results to make an insert into the new table. I want to make a new table from these information.



      table clients:



      id | email          | status
      ----------------------------
      1 | user1@mail.com | Active
      2 | user2@mail.com | Inactive
      3 | user3@mail.com | Active


      table tblhosting



      id | userid | packageid | domainstatus 
      ------------------------------------------------
      1 | 1 | 2 | Active
      2 | 2 | 3 | Active
      3 | 3 | 1 | Active


      table tblproducts



      id | name 
      -----------
      1 | Basic
      2 | Silver
      3 | Gold


      I expect result like :



      id | user_id | email          | pack_id | pack_name | quota
      -----------------------------------------------------------
      1 | 1 | user1@mail.com | 2 | Silver | 100
      2 | 2 | user2@mail.com | 3 | Gold | 1000
      3 | 3 | user3@mail.com | 1 | Basic | 10


      If I put max in the case statment, it will work but will not show all data.










      share|improve this question













      I have this procedure :



      BEGIN 
      DECLARE done INT DEFAULT FALSE;
      DECLARE `id_var` varchar(255);
      DECLARE `cur1` CURSOR FOR
      SELECT `id` FROM `clients`
      WHERE `status` = 'Active';
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
      DROP TABLE IF EXISTS `tblquota_nc`;
      CREATE TABLE IF NOT EXISTS `tblquota_nc` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `user_id` int(11) NOT NULL,
      `email` varchar(255),
      `pack_id` int(11) NOT NULL,
      `pack_name` varchar(255) NOT NULL,
      `quota` int(11) NULL,
      PRIMARY KEY (`id`)
      );
      OPEN cur1;
      read_loop: LOOP
      FETCH NEXT
      FROM cur1
      INTO id_var;
      IF done THEN
      LEAVE read_loop;
      END IF;

      SELECT clients.id as `User ID`, `email`, `packageid` as `Pack ID`, `name` as `Pack`, (CASE
      WHEN `name` = "Basic" THEN '10'
      WHEN `name` = "Silver" THEN '100'
      WHEN `name` = "Gold" THEN '1000'
      ELSE '10'
      END) as Quota INTO @mid, @mail, @p_id, @packname, @quota
      FROM `clients`
      INNER JOIN `tblhosting` ON clients.id = tblhosting.userid
      INNER JOIN `tblproducts` ON tblhosting.packageid = tblproducts.id
      WHERE clients.status = 'Active'
      AND tblhosting.domainstatus = 'Active'
      AND clients.id = id_var;
      IF (SELECT id FROM tblquota_nc WHERE user_id = @mid) THEN
      BEGIN
      END;
      ELSE
      BEGIN
      if (@mid IS NOT NULL AND @p_id IS NOT NULL AND @packname IS NOT NULL) then
      INSERT INTO tblquota_nc (user_id, email, pack_id, pack_name, quota) VALUES (@mid, @mail, @p_id, @packname, @quota);
      end if;
      END;
      END IF;
      END LOOP;
      CLOSE cur1;
      END


      It seems like I have an error because the SELECT statment return several values. I thought doing another loop with these results to make an insert into the new table. I want to make a new table from these information.



      table clients:



      id | email          | status
      ----------------------------
      1 | user1@mail.com | Active
      2 | user2@mail.com | Inactive
      3 | user3@mail.com | Active


      table tblhosting



      id | userid | packageid | domainstatus 
      ------------------------------------------------
      1 | 1 | 2 | Active
      2 | 2 | 3 | Active
      3 | 3 | 1 | Active


      table tblproducts



      id | name 
      -----------
      1 | Basic
      2 | Silver
      3 | Gold


      I expect result like :



      id | user_id | email          | pack_id | pack_name | quota
      -----------------------------------------------------------
      1 | 1 | user1@mail.com | 2 | Silver | 100
      2 | 2 | user2@mail.com | 3 | Gold | 1000
      3 | 3 | user3@mail.com | 1 | Basic | 10


      If I put max in the case statment, it will work but will not show all data.







      mysql sql stored-procedures






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 9:25









      executable

      1,0741221




      1,0741221
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I don't think you need a stored procedure to do this. Just use CREATE TABLE ... SELECT syntax:



          CREATE TABLE tblquota_nc (id INT AUTO_INCREMENT PRIMARY KEY) AS
          SELECT c.id as user_id
          , c.`email`
          , h.`packageid` as pack_id
          , p.`name` as pack_name
          , (CASE WHEN `name` = "Basic" THEN '10'
          WHEN `name` = "Silver" THEN '100'
          WHEN `name` = "Gold" THEN '1000'
          ELSE '10'
          END) as quota
          FROM `clients` c
          LEFT JOIN `tblhosting` h ON c.id = h.userid
          INNER JOIN `tblproducts` p ON h.packageid = p.id
          ORDER BY c.id;


          Output from SELECT * FROM tblquota_nc:



          id  user_id     email           pack_id     pack_name   quota
          1 1 user1@mail.com 2 Silver 100
          2 2 user2@mail.com 3 Gold 1000
          3 3 user3@mail.com 1 Basic 10


          Demo on dbfiddle






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371595%2fsql-procedure-multiple-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            I don't think you need a stored procedure to do this. Just use CREATE TABLE ... SELECT syntax:



            CREATE TABLE tblquota_nc (id INT AUTO_INCREMENT PRIMARY KEY) AS
            SELECT c.id as user_id
            , c.`email`
            , h.`packageid` as pack_id
            , p.`name` as pack_name
            , (CASE WHEN `name` = "Basic" THEN '10'
            WHEN `name` = "Silver" THEN '100'
            WHEN `name` = "Gold" THEN '1000'
            ELSE '10'
            END) as quota
            FROM `clients` c
            LEFT JOIN `tblhosting` h ON c.id = h.userid
            INNER JOIN `tblproducts` p ON h.packageid = p.id
            ORDER BY c.id;


            Output from SELECT * FROM tblquota_nc:



            id  user_id     email           pack_id     pack_name   quota
            1 1 user1@mail.com 2 Silver 100
            2 2 user2@mail.com 3 Gold 1000
            3 3 user3@mail.com 1 Basic 10


            Demo on dbfiddle






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              I don't think you need a stored procedure to do this. Just use CREATE TABLE ... SELECT syntax:



              CREATE TABLE tblquota_nc (id INT AUTO_INCREMENT PRIMARY KEY) AS
              SELECT c.id as user_id
              , c.`email`
              , h.`packageid` as pack_id
              , p.`name` as pack_name
              , (CASE WHEN `name` = "Basic" THEN '10'
              WHEN `name` = "Silver" THEN '100'
              WHEN `name` = "Gold" THEN '1000'
              ELSE '10'
              END) as quota
              FROM `clients` c
              LEFT JOIN `tblhosting` h ON c.id = h.userid
              INNER JOIN `tblproducts` p ON h.packageid = p.id
              ORDER BY c.id;


              Output from SELECT * FROM tblquota_nc:



              id  user_id     email           pack_id     pack_name   quota
              1 1 user1@mail.com 2 Silver 100
              2 2 user2@mail.com 3 Gold 1000
              3 3 user3@mail.com 1 Basic 10


              Demo on dbfiddle






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                I don't think you need a stored procedure to do this. Just use CREATE TABLE ... SELECT syntax:



                CREATE TABLE tblquota_nc (id INT AUTO_INCREMENT PRIMARY KEY) AS
                SELECT c.id as user_id
                , c.`email`
                , h.`packageid` as pack_id
                , p.`name` as pack_name
                , (CASE WHEN `name` = "Basic" THEN '10'
                WHEN `name` = "Silver" THEN '100'
                WHEN `name` = "Gold" THEN '1000'
                ELSE '10'
                END) as quota
                FROM `clients` c
                LEFT JOIN `tblhosting` h ON c.id = h.userid
                INNER JOIN `tblproducts` p ON h.packageid = p.id
                ORDER BY c.id;


                Output from SELECT * FROM tblquota_nc:



                id  user_id     email           pack_id     pack_name   quota
                1 1 user1@mail.com 2 Silver 100
                2 2 user2@mail.com 3 Gold 1000
                3 3 user3@mail.com 1 Basic 10


                Demo on dbfiddle






                share|improve this answer












                I don't think you need a stored procedure to do this. Just use CREATE TABLE ... SELECT syntax:



                CREATE TABLE tblquota_nc (id INT AUTO_INCREMENT PRIMARY KEY) AS
                SELECT c.id as user_id
                , c.`email`
                , h.`packageid` as pack_id
                , p.`name` as pack_name
                , (CASE WHEN `name` = "Basic" THEN '10'
                WHEN `name` = "Silver" THEN '100'
                WHEN `name` = "Gold" THEN '1000'
                ELSE '10'
                END) as quota
                FROM `clients` c
                LEFT JOIN `tblhosting` h ON c.id = h.userid
                INNER JOIN `tblproducts` p ON h.packageid = p.id
                ORDER BY c.id;


                Output from SELECT * FROM tblquota_nc:



                id  user_id     email           pack_id     pack_name   quota
                1 1 user1@mail.com 2 Silver 100
                2 2 user2@mail.com 3 Gold 1000
                3 3 user3@mail.com 1 Basic 10


                Demo on dbfiddle







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 at 9:36









                Nick

                20.7k51434




                20.7k51434






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371595%2fsql-procedure-multiple-value%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                    Alcedinidae

                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]