How to write sql table output at a specific range or cell.

sarita kumari 1 Reputation point
2020-08-18T16:47:03.733+00:00

I need to export Sql server data into Excel at a specific range. Below is the query I am using:-

INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;
Database=C:\Users\Documents\Discrepancy Report.xlsx;','SELECT * FROM [Sheet1$A22:C37]')
select * from [Test]

It is throwing error as
Cannot process the object "SELECT * FROM [Sheet1$A22:C37]". The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" indicates that either the object has no columns or the current user does not have permissions on that object.

How can i do this

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
Developer technologies Transact-SQL
SQL Server Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Guoxiong 8,206 Reputation points
    2020-08-18T18:04:51.767+00:00

    Go to the "SQL Server (MSSQLSERVER)" service and change the service account from NT Service\MSSQLSERVER to Local System and then restart the service. If you want to export the specific table columns to the excel, you need to have the column headers in the excel sheet. And then run the following query:

    INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;
    Database=C:\Users\Documents\Discrepancy Report.xlsx;','SELECT * FROM [Sheet1$]')
    SELECT Col1, Col2, ... FROM [Test]
    

    You need to specify the names of the columns you want to export data from .

    0 comments No comments

  2. Monalv-MSFT 5,926 Reputation points
    2020-08-19T02:13:13.377+00:00

    Hi saritakumari,

    We can use OLEDB Source and Excel Destination in SSIS package.
    18662-df.png

    When configuring Excel Destination, we can use the following two methods to get specific range:
    Method1: Set AccessMode as SQL Command
    18671-sqlcommand1.png
    18635-sqlcommand2.png

    Method2: Set AccessMode as OpenRowset
    18681-openrowset1.png
    18682-openrowset2.png

    Please refer to How to read data from an Excel file starting from the nth row with SQL Server Integration Services.

    Best Regards,
    Mona

    ----------

    If the response is helpful, please click "Accept Answer" and upvote it.


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.