Active Member
November 19, 2020
I've got a number of SQL queries that all reference a shared temp table - let's call it ##TEMP_TABLE.
Not exactly an issue in SSMS, but how do I create a global temp table that can be referenced by the other SQL queries in Power Query?
- I tried creating a connection only, but it's really really slow as the temp table is HUGE which makes it a non-starter
- In an ideal non-political world, I would create a view on the server and reference the view, but this isn't possible here.
Any thoughts clever people?
July 16, 2010
Hi Martin,
Welcome to our forum!
I'm not sure what you mean by 'I've got a number of SQL queries...', where are these queries? My reply below assumes the ##TEMP_TABLE is a Power Query query.
You can export your queries to an ODV file and then share them across your workbooks or with co-workers. To export a query definition into an ODC file in Excel 2016 or later, right-click on a query in the Queries & Connections side pane and choose Export Connection File.
To use the query and import data via the ODC mechanism, click Data > Existing Connections. The Existing Connections dialog will appear, and you can then import from the available ODC files, existing workbook connections or tables.
That said, I'm not sure this will improve performance. If the temp table query is slow, then it'll still be slow here, so perhaps it's better to fix why the query is slow.
Mynda
Active Member
November 19, 2020
Hi Mynda
Thanks for the reply and greeting.
Sorry I thought I had made myself clear in the question. My bad! 🙂
My temp table is of the form
SELECT * INTO ##TEMP_TABLE
Obviously much more complicated than that in the real world.
I then have a series of SQL queries inside Power Query that ideally need to reference ##TEMP_TABLE.
That's the bit I don't know how to do - or even if it's possible.
Now I could write the temp table code into every table but that would really slow it down. I can import data too.
My question though, is can I employ a global temporary table for SQL server? Is it possible?
Thanks
October 5, 2010
Hi Martin,
I understand the purpose of the ##TEMP table in SQL Server but are you sure it is necessary to create one in PQ?
I obviously don't know how much data we are talking about, but the way PQ works is that it only loads the data it needs. Particularly with something like a SQL database.
Initially you will load, for example, the entire table (actually PQ only loads a preview, not the whole lot) and then filter out what you don't want until you get the data you do want.
PQ then creates its own SQL commands to get the SQL server to do the SELECT, filtering, grouping etc. PQ pushes this back to the SQL server (this is called query folding) so that the SQL server does the bulk of the work and only transfers the data you want.
If you write your own SQL commands in PQ then query folding won't work and generally this makes the query performance worse.
In your case, have you tried just creating a query in PQ and then doing a refresh to see how long it takes?
Regards
Phil
October 5, 2010
Hi Martin,
You can write a query to create a "temp table" in PQ, but if you are doing this in Excel, the query will load the data from SQL every time you refresh queries.
If you do this in PBI, you can turn off refresh for the query so you'll work with a 'static' table of data.
But once you have this table in PQ you should be using native M to interrogate the data.
Regards
Phil
October 5, 2010
Hi Martin,
I'm not sure there is an equivalent in PQ to a Global Temp Table, and I'm not sure how you intend to make use of one.
If you create a query in an Excel workbook, or in PBI, the result of that query can be used by other queries in that workbook or PBI file, no problem there. You just refer to the query by name to access it's result, whether that's a table, a list or whatever.
However a query (the temp table) created in one workbook can't be accessed directly by queries in another workbook. You'd need to actually copy/paste the query code between workbooks or Export the Connection File for the query, then import that into your new workbook - essentially the same as just copy/paste.
Regards
Phil
1 Guest(s)