Forum Discussion

crejo's avatar
crejo
Regular Visitor
2 years ago
Solved

Loading multiple tables from one SQL query

Take the following PowerQuery M code:

 

 

 

let
  QueryResult = Sql.Database("Server", "database", [Query="select * from MyTable1; select * from MyTable2;"])
in
  QueryResult

 

 

 

This will output One table (the result of select * from MyTable1). Is there a way to get the result from both tables, maybe in a list?

 

Thank you

  • Then you would have to package them (for example as JSON or XML) and send them over as a string that you would then untangle in Power Query.  And this is where the fun stops - Power Query cannot create multiple tables from a single query.

     

    What's your business case?

8 Replies

  • Do these tables have the same structure? In which case you could say

     

    let
      QueryResult = Sql.Database("Server", "database", [Query="select * from MyTable1 union all  select * from MyTable2;"])
    in
      QueryResult
    • crejo's avatar
      crejo
      Regular Visitor

      The tables do not have the same structure unfortunately.

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        Then you would have to package them (for example as JSON or XML) and send them over as a string that you would then untangle in Power Query.  And this is where the fun stops - Power Query cannot create multiple tables from a single query.

         

        What's your business case?

    • crejo's avatar
      crejo
      Regular Visitor

      The 2 output tables are based on the same collected data. I want to avoid having to rerun the same expensive query twice.

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        The SQL Server Query cache should take care of that.

  • You input comes from 2 tables. In terms of query cost, the difference between 1 query over 2 tables or 2 queries each over 1 table will be negligible with sufficiently large tables. At the end of the day all data needs to be retrieved.