SQL Server export to Excel with OPENROWSET
You'd have to use dynamic SQL. OPENROWSET
etc only allows literals as parameters.
DECLARE @myfile varchar(800)
SET @myfile = 'C:\template.xls'
EXEC ('
insert into OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',
''Excel 8.0;Database=' + @myfile + ';'',
''SELECT * FROM [SheetName$]'')
select * from myTable
')
Remember: the path is relative to where SQL Server is running
Export SQL result to Excel by using OPENROWSET, but provider has not been registered
to summarize:
In order to use OPENROWSET for export/import excel files from/to x64 SQL Server you should install Access Database Engine 2010 Redistributable on your server, not on your workstation. Until you successfully install this x64 bit driver on your server pc you'll get the error
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" has not been registered
while trying to use this provider in your OPENROWSET queries
Using OPENROWSET to export dataset into specific range in Excel sheet
There is no way how to do this via opendatasource/openrowset...
Opendatasource is half-unproduced functionality. It's probably not done to work with data but only for ad-hoc connections.
Related Topics
What Do You Do in SQL Server to Create or Alter
Truncate Table Within Transaction
What Is the Linq Equivalent to the SQL in Operator
Oracle: SQL Query to Find All the Triggers Belonging to the Tables
Rails/Postgres: "Must Appear in the Group by Clause or Be Used in an Aggregate Function"
Rails .Where() Query Not Working
How to Connect to SQL Server 2005 Database Through Ruby
Unexpected Results from SQL Query with Between Timestamps
Ruby on Rails - Search in Database Based on a Query
SQL Efficient Way to Join a Table Where All Values Exist
Why Does the Following Join Increase the Query Time Significantly
Some Sort of "Different Auto-Increment Indexes" Per a Primary Key Values
Select One Row Per Index Value with Max Column Value
Postgres Interval Using Value from Table
Select Distinct Is Slower Than Expected on My Table in Postgresql