INSERT INTO OPENROWSET to insert data to a excel by SQL query

SVA 116 Reputation points
2023-05-11T18:15:57.64+00:00

Hi

I am using sql server 2017. Please help me to export data to excel by running a query.

Thx

SQL Server | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.7K Reputation points MVP Volunteer Moderator
    2023-05-11T21:12:29.97+00:00

    In Excel, find the Data tab on the menu. In the Ribbon select Get Data. In the dropdown, select From Database and then From SQL Server Database. Specify server and database, open Advanced options. Here you can enter your query. Press OK and then Load in the next dialog.

    I've never tried loading Excel through INSERT OPENROWSET, but I would expect that many stars need to be aligned for to work out.

    If this is something you want to do on a regular basis, I would look for away to automate the steps for Excel through VBA.


  2. Olaf Helper 47,441 Reputation points
    2023-05-12T04:39:50.97+00:00

    Please help me to export data to excel by running a query.

    With plain T-SQL? Better use a ETL tool like SSIS:

    https://learn.microsoft.com/en-us/sql/integration-services/load-data-to-from-excel-with-ssis?view=sql-server-ver16

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.