Forum Discussion
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
- lbendlin
Super User
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- crejoRegular Visitor
The tables do not have the same structure unfortunately.
- lbendlin
Super 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?
- PwerQueryKees
Super User
Why not have 2 sql queries? What are you trying to achieve?
- PwerQueryKees
Super User
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.