Search code examples
mysqlsqljoinprocedure

How to combine 8 queries into one to create a procedure?


I have a table I created to match an online Template for uploading inventory to Amazon. In total, it has 440 columns. I'm not worried about that, and neither are they, it is mostly necessary. It pulls from two other tables that I'll call table1 and table2. I'll call the other one templateTable.

Basically, I'm starting with a TRUNCATE to completely wipe the information on the templateTable. I want it empty when it gets filled, for no reason other than it makes me feel comfortable. No other table gets truncated, just this table every time the query is run.

After that, there is a massive INSERT query that takes info from table1 and table2 and puts all of that into templateTable's specific columns.

Query 3 is an update at this point, and so is pretty much queries 3 - 8. They're all update queries. I did them separately from the second query, where everything gets populated, because each update has a CASE and different requirements.

I wanted to create a procedure for these queries so they could just run the one procedure and call it a day. But I'm uncertain how to combine the 8 queries that fill and correct the information in this templateTable. I should mention I'm not just taking info from one table and sticking it in the templateTable- it is more like "case when table1.modelNum = 1234 then templateTable.modelNum = 5678".


Solution

  • You can wrap all of your 8 SQL statements inside of a procedure in MySQL like this:

    CREATE PROCEDURE MyProcedure()
    BEGIN 
        <SQL STATEMENT 1>;
        <SQL STATEMENT 2>;
        <SQL STATEMENT 3>;
        <SQL STATEMENT ...>;
    END//
    

    Then to call it you submit:

    exec MyProcedure;