Importing sql query output in excel

AbdulWahab Khan 41 Reputation points
2022-09-06T13:03:50.087+00:00

I have few queries writtern in different rows in excel like
In Sheet1
cell A1 - SELECT * FROM DBO.EMP
cell A2 - select * from dbo.dept
cell A3 -
select * from dbo.emp
except
select * from sa.emp
cell A4 - select * from sa.loc

now I want these queries to be fired in some test db in sql server 2017,

and the output for query should be writtern in new sheet (sheet2) like
in cell A1 - query should be there
and from cell B1 , output should get printed
The next query should get printed after the first query output. Like if the first query results ends in row - B12,
the second query output should start from row B13, and the second query in the new sheet should be displayed in cell A13
Similarly for other queries writtern in sheet1

I Dont want to use any VBA code for this.

Office version - Microsoft office Profession Plus 2016
Database Tool Version- SQL Server 2017

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,642 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,459 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,638 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,547 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 40,656 Reputation points
    2022-09-06T14:07:21.18+00:00

    I Dont want to use any VBA code for this.

    There is no build-in feature in MS Excel to achieve this; VBA (or a .NET app) is the only way I know.


  2. Jingyang Li 5,891 Reputation points
    2022-09-06T16:04:23.23+00:00

    You can look into Powershell or Python for solutions as well.

    0 comments No comments

  3. Ceasar Chen_MSFT 4,391 Reputation points
    2022-09-07T03:04:06.837+00:00

    Hi, @AbdulWahab Khan
    Entering sql statements in cells to query this may not be possible. The recommended approach is to enter a statement in the dialog box to query after connecting to the database.
    238423-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments