Introduction
There are couple of ways to import data from Excel to SSMS – via SQL Server Import and Export Wizard (where you should have first installed the OLEDB provider),via SQL Server Integration Services (SSIS), via Distributed queries, via Linked servers and others. You can read more about them from the Microsoft Doc: Import data from Excel to SQL – SQL Server | Microsoft Docs. However, in this article I would like to focus your attention to one quite peculiar method that I am almost sure you cannot find online. This is a method I have learned from a colleague. This method could be used if you want to quickly examine some columns from Excel into Management Studio, without necessarily importing them.
The Method
For this purpose, we will use sample data from this source: Excel Sample Data for Testing and Examples (contextures.com). After we download and open the file, we will navigate to the SalesOrders tab and we will start writing the following SQL query in Excel:
="SELECT '"&A2&"' AS OrderDate, '"&B2&"' AS Region, '"&C2&"' AS Rep, '"&D2&"' AS Item, '"&E2&"' AS Units, '"&F2&"' AS UnitCost, '"&G2&"' AS Total UNION ALL"
Before dragging the formula and populating the other rows, first we must pay attention to our date column:
As we can see, since we are reading the data in the SQL query as a string, we cannot initially display it in our desired date format. We should make some date conversions either in Excel, or later on in Management Studio. Since the conversion would be a little bit easier in Excel, I would prefer to do it in this program. We simply add another column, where we type the following formula: = TEXT([@OrderDate],"dd/mm/yyyy")
After we rearrange our columns, save the text formula column as values, rename it, we can delete the original OrderDate column because we do not need it anymore. Then we can simply drag the Select formula until the bottom of the table, in order to populate all the rows. On the last row, it is very important to remove the UNION ALL operator, otherwise the query will not be executed successfully when copying the script into Management Studio.
As a next step, we just copy the script from all the rows, paste it in our Management Studio, and execute it.
Afterwards we can import the output into a table that we could access and query easily. If we are working in a Synapse environment, the script would be like:
CREATE TABLE [Ref].SampleData WITH ( DISTRIBUTION = ROUND_ROBIN, CLUSTERED COLUMNSTORE INDEX ) AS SELECT * FROM (<here we put the multiple select statements>) s
If not in a Synapse environment, the syntax would be like:
SELECT * INTO SampleData
FROM(<the multiple select statements>) s
Drawbacks
The drawbacks of this method are the following:
- If we are working with many columns, it could be quite frustrating to type or even copy the repeating algorithm of the formula. We need to be quite careful and pay attention to the sequence of columns we are referring to.
- Management Studio does not like the possessive form of the strings, and we must manually add an additional apostrophe, in order to remediate the problem:
As you can guess, that could be such a pain in the neck when we are working with a large volume of such data.
Conclusion
All in all, I believe that this method, although a bit strange at first glance, is an interesting alternative to the other import options, which might not necessarily work every time due to some data or file format discrepancies. Of course, this method is not ideal, but in some cases, it could be even faster and more efficient than the other methods.
Click here to read our other blogs.
Building Data Apps with Python’s Streamlit
Off-the-shelf solutions for interactive data app development such as Microsoft Power BI are great –
Nov
Power BI to Power AI – Part 3
This post is the third part of a blog series on the AI features of
Nov
Data Factory in Fabric
As data engineers, we use Azure Data Factory on a daily basis to collect datasets
Nov
Microsoft Fabric Is Generally Available
Microsoft Fabric is now Generally Available What has happened? On the 15th November Microsoft announced
Nov
An Introduction to Semantic Link in Microsoft Fabric
Having previously worked in the water & energy industries, I recently created a utilities PowerBI
Nov
Prompt Engineering and Creating a Python Function to Query OpenAI
Artificial intelligence tools, like Chat GPT, are all the buzz right now, and rightfully so
Nov
Exploring different options for collaborative workspaces: Hex, Hyperquery & Deepnote
Real-time collaboration, code completion and data versioning are just a few of the latest capabilities
Nov
NHS Blood and Transplant Shortlisted for Government Project Delivery Awards
We are delighted to share that NHS Blood and Transplant’s data platform project has been
Nov