How to export sql server table data to excel sheet.
insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=D:\Book1.xlsx;',
'SELECT * FROM [SheetName$]') select TOP 5 CustomerID from Customers
I used the above query but it shows following error
Msg 7308, Level 16, State 1, Line 1 OLE DB provider
'Microsoft.ACE.OLEDB.12.0' cannot be used for distributed queries
because the provider is configured to run in single-threaded apartment
mode.
I have found the solution
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO
EXEC sp_MSSet_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
GO
EXEC sp_MSSet_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
GO
insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=D:\Book1.xls;',
'SELECT * FROM [Sheet1$]') select TOP 5 CustomerID from Customers
Its working fine
Related
I am trying to import data from Excel into SQL.
I saw all kinds of answers on Google, but I didn't really find the right answer
I would really appreciate any help on this matter...
I have an excel file in a folder and I want to create a script that will load all the data from this excel file into a dedicated table in my DB
The script should be written in SQL Server
Does anyone have a simple way to do this?
I try this code:
sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO
USE Diyur;
GO
SELECT * INTO Data_dq
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=C:\File.xls', [Sheet1$]);
GO
But I get this error:
Msg 7403, Level 16, State 1, Line 12 The OLE DB provider
"Microsoft.ACE.OLEDB.12.0" has not been registered. Blockquote
After many google searches and my many attempts I found the correct code:
declare #SQL nvarchar(max) = '
CREATE TABLE #TempTable
( [Field1] nvarchar(max) NULL,
[Field2] nvarchar(max) NULL,
[Field3] nvarchar(max) NULL );
BULK INSERT #TempTable FROM ''<FullPath>\FileName.csv'' WITH --if the path is in the network - need to write the Full-path of the drive
(
KEEPIDENTITY,
FIELDTERMINATOR = '','',
MAXERRORS = 10000,
KEEPNULLS,
ROWTERMINATOR=''\n'',
FIRSTROW = 2,
CODEPAGE = ''1255''
);
select * from #TempTable
Insert into TableNameInDB(Field1,Field2,Field3)
select * from #TempTable
'
EXEC sp_executesql #SQL
I imported an Excel (.xlsx) file into a table in SQL Server using the import wizard.
I want to get the query used to import so that I can store it and incorporate it in a SQL Server stored procedure. How can I get that query?
You can't get the specific query used by the wizard, you can only save the SSIS package for later use. You can do this with other methods, via sql, which you can include in your stored procedure as explained in the official documentation.
OPENROWSET
sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO
USE ImportFromExcel;
GO
SELECT * INTO Data_dq
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=C:\Temp\Data.xlsx', [Sheet1$]);
GO
BULK INSERT
USE ImportFromExcel;
GO
BULK INSERT Data_bi FROM 'C:\Temp\data.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
GO
This is code does not run in the Crystal Report.
Error:Failed to retrieve data from the database. Details: ADO Error Code 0x80040e14 Source Microsoft SQL Server Native Client 11.0 Description: Incorrect syntax near isp_configure. SQL State 42000 Native Error: 102 [Database Vendor Code: 102
Declare #ReportDate DateTime
Set #ReportDate={?ReportDateR}
Select * from [SBO_SPEL].[dbo].[U_SPEL_SALES_ANALYSIS_TARGET&ACTUAL_UNIT_F] (#ReportDate)
UNION ALL
Select * from [SBO_SPEL].[dbo].[U_SPEL_SALES_ANALYSIS_TARGET&ACTUAL_SE_F] (#ReportDate)
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 32768;
GO
RECONFIGURE;
GO
WAITFOR DELAY '00:02:00'
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 131072;
GO
RECONFIGURE;
GO
Crystal returns to the report the result set from the last SQL statement in a Command or SP. But your last SQL statement has no result set.
I'm upgrading our reporting server from 2008 R2 32-bit to 2014 32-bit.
We have linked servers that are excel files. These are .xls files (although the problem persists when they are updated to .xlsm). The connects work fine (select & insert into); however, when we run a stored procedure to create a new tab in the excel file.
USE [SupplementaryReports]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spExecute_ADODB_SQL]
#DDL VARCHAR(8000),
#DataSource VARCHAR(500),
#Worksheet VARCHAR(200)=NULL,
#ConnectionString VARCHAR(255) = 'Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=%DataSource;
Extended Properties=Excel 12.0'
AS
DECLARE
#objExcel INT,
#hr INT,
#command VARCHAR(255),
#strErrorMessage VARCHAR(255),
#objErrorObject INT,
#objConnection INT,
#bucket INT
SELECT #ConnectionString =REPLACE (#ConnectionString, '%DataSource', #DataSource)
IF #Worksheet IS NOT NULL
SELECT #DDL=REPLACE(#DDL,'%worksheet',#Worksheet)
SELECT #strErrorMessage='Making ADODB connection ',
#objErrorObject=NULL
EXEC #hr=sp_OACreate 'ADODB.Connection', #objconnection OUT
IF #hr=0
SELECT #strErrorMessage='Assigning ConnectionString property "'
+ #ConnectionString + '"',
#objErrorObject=#objconnection
IF #hr=0 EXEC #hr=sp_OASetProperty #objconnection, 'ConnectionString', #ConnectionString
IF #hr=0 SELECT #strErrorMessage ='Opening Connection to XLS, for file Create or Append'
IF #hr=0 EXEC #hr=sp_OAMethod #objconnection, 'Open'
IF #hr=0 SELECT #strErrorMessage ='Executing DDL "'+#DDL+'"'
IF #hr=0 EXEC #hr=sp_OAMethod #objconnection, 'Execute',
#Bucket out , #DDL
IF #hr<>0
BEGIN
DECLARE
#Source VARCHAR(255),
#Description VARCHAR(255),
#Helpfile VARCHAR(255),
#HelpID INT
EXECUTE sp_OAGetErrorInfo #objErrorObject, #source output,
#Description output,#Helpfile output,#HelpID output
SELECT #strErrorMessage='Error whilst '
+COALESCE(#strErrorMessage,'doing something')+', '
+COALESCE(#Description,'')
RAISERROR (#strErrorMessage,16,1)
END
EXEC #hr=sp_OADestroy #objconnection
GO
I get the following error
Msg 15281, Level 16, State 1, Procedure sp_OACreate, Line 116
SQL Server blocked access to procedure 'sys.sp_OACreate' of component 'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', search for 'Ole Automation Procedures' in SQL Server Books Online.
Msg 15281, Level 16, State 1, Procedure sp_OADestroy, Line 116
SQL Server blocked access to procedure 'sys.sp_OADestroy' of component 'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', search for 'Ole Automation Procedures' in SQL Server Books Online.
This error often means that ad hoc distribtued queries are not enough. But they are (both 'show advanced options' and 'ad hoc distributed queries' are set to 1). We also have the provider Microsoft.ACE.OLEDB.12.0 with allow in process and dynamic paramteres enabled.
The excel files work perfectly with 2008 R2. The code itself hasn't been changed. All that's changed is upgrading to 2014. Does anyone have any suggestions? You know... besides not using SQL to connect to excel files since we don't have the resources to change that.
Much obliged!
The most likely answer is Ole Automation Procedures is not turned on. You can check by running:
EXEC sp_configure 'Ole Automation Procedures';
This will return something along the lines of:
name minimum maximum config_value run_value
Ole Automation Procedures 0 1 0 0
If the config_value is 0 you need to enable it.
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
Source.
I'm trying to write a batch file that takes information from an Excel spreadsheet and adds new rows into my SQL Server.
I think what I have currently is nearly there. As it runs and says a line has been updated, but it hasn't.
This is my batch file:
#echo off
copy \\RDS-2012-C\2012_N_Drive\Operations\Reports\Metrics\sql2.xlsx C:\ACCESS_SQLSVR\import_location
sqlcmd -S RDS-2012-G\RDS_SQL -d Master -i C:\ACCESS_SQLSVR\import_location\sesame_import.sql
pause
And this is what it runs:
EXEC sp_configure 'Show Advanced Options', 1;
RECONFIGURE
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE
GO
EXEC sp_MSSet_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1;
GO
EXEC sp_MSSet_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1;
GO
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\ACCESS_SQLSVR\import_location\sql2.xlsx;HDR=YES;IMEX=1',
'SELECT * FROM [SQL$]')
GO
Do I simply need a different command instead of SELECT?
Also, the full database and table in the SQL Server is called:
Sesame_FTest.dbo.SQL
Running this when the table doesn't exist results in the table being created and filled with the Excel information. But running it a second time with a new Excel doesn't update like I thought it would.
Try it like this . . .
------ INSERT INTO NON-EXISTING TABLES
USE [YourDatabase]
GO
SELECT * INTO tbl_Mapping
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;Database=C:\Users\rshuell001\Desktop\State Street\Final Deliverables\Raw Data\Mapping.xlsx',
'SELECT * FROM [Mapping$]')
SELECT * INTO tbl_Data
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;Database=C:\Users\rshuell001\Desktop\State Street\Final Deliverables\Raw Data\Data.xlsx',
'SELECT * FROM [Data$]')
OR . . .
------ INSERT INTO EXISTING TABLES
USE [YourDatabase]
GO
Insert INTO tbl_Mapping
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;Database=C:\Users\rshuell001\Desktop\State Street\Final Deliverables\Raw Data\Mapping.xlsx',
'SELECT * FROM [Mapping$]')
Insert INTO tbl_Data
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;Database=C:\Users\rshuell001\Desktop\State Street\Final Deliverables\Raw Data\Data.xlsx',
'SELECT * FROM [Data$]')