In my previous post as shown here, I have shown how to load data from a excel sheet into SQL DB.
In this post, let me show query to load data from SQL DB to Excel sheet.
As in earlier post, we can use OPENROWSET Command to load SQL Server DB into Excel sheet.
I have table 'Management' table in 'Test' Database and I wanted to load Management Table Data into a excel sheet.
Snippet shown below shows Management table's data:
My Excel sheet 'Dest' which carries column name and snippet of the same is shown below:
The Query to load SQL Server DB Data into Excel sheet is below:
In this post, let me show query to load data from SQL DB to Excel sheet.
As in earlier post, we can use OPENROWSET Command to load SQL Server DB into Excel sheet.
I have table 'Management' table in 'Test' Database and I wanted to load Management Table Data into a excel sheet.
Snippet shown below shows Management table's data:
My Excel sheet 'Dest' which carries column name and snippet of the same is shown below:
The Query to load SQL Server DB Data into Excel sheet is below:
INSERT
INTO
OPENROWSET
('Microsoft.ACE.OLEDB.12.0',
'Excel
12.0 Xml;Database=C:\Users\dhinakaran\Desktop\Dest.xlsx;',
'SELECT
* FROM [Sheet1$]')
SELECT
ID,
NAME,
DESIGNATION
FROM
Test.dbo.Management
After running this query, excel sheet is loaded with Management table Data.
The snippet is as shown below:
If you get the error:
Msg
15281, Level 16, State 1, Line 2
SQL
Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of
component 'Ad Hoc Distributed Queries' because this component is
turned off as part of the security configuration for this server. A
system administrator can enable the use of 'Ad Hoc Distributed
Queries' by using sp_configure. For more information about enabling
'Ad Hoc Distributed Queries', search for 'Ad Hoc Distributed Queries'
in SQL Server Books Online. Follow this link
Comments
Post a Comment