Silent truncation using OpenRowSet to Bulk Load data - sql-server

I‌‌ have to load CSV data into SQL server table using OpenRowSet.
‌ I have installed the AccessDatabaseEngine_X64.exe, Access and SQL server 2014 both are 64-bit.
Also enabled below settings,
sp_configure 'show advanced options', 1
reconfigure with override
sp_configure 'Ad Hoc Distributed Queries', 1
reconfigure with override
INSERT INTO dbo.Test
SELECT * FROM OPENROWSET('MSDASQL'
,'Driver={Microsoft Access Text Driver (*.txt,*.csv)}','select * fromD:\MYDATA\go\test.CSV')
‌‌
D‌ata was also getting loaded into the table, but some of my rows in CSV have data more than what is defined in the schema of the table (Test) and I don't want to change column size in the table(Test) So SQL started giving errors related to truncation. Without using OpenRowSet earlier I used to use "Set ANSI WARNINGS OFF" to do silent truncation. But with OpenRowSet if I use this command then it gives below error
Heterogeneous queries require the ANSI_NULLS and ANSI_WARNINGS options to be set for the connection. This ensures consistent query semantics. Enable these options and then reissue your query.‌‌
I want to use OpenRowSet because it is very fast. So can anyone please help me on how I can do silent truncation using OpenRowSet.

Related

Enabling SQL Ad Hoc Distributed Queries for a single command

I need to read data from Excel files to make hundreds of updates to SQL Server. The server is by default configured to not allow these queries, but I can change this setting. I read about the risks of allowing these queries, but I was wondering if there are security concerns or performance issues or other risks I don't know about, if I enable them for the duration of a single command.
In short, is it reasonable to do the following?
exec sp_configure 'Ad Hoc Distributed Queries', 1
reconfigure
begin try
select * from OPENROWSET(
'Microsoft.ACE.OLEDB.12.0'
,'Excel 12.0;Database=C:\Temp\Test.xlsx;'
,'SELECT * FROM [Sheet1$]') as xl
end try
begin catch
print 'Error occurred'
end catch
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
I would of course wrap this in a stored procedure and use it many times over for different source and target tables.
Answering my own question based on my own findings, the answer is no, the above is not quite reasonable.
Apparently the GO keyword is necessary at least after the reconfigure statements. I couldn't get a successful test as the server machine did not have the ACE OleDb provider installed.
Also, try-catch does not catch errors relating to the missing provider. So if someone can tell how this could be done safely, please do.

SQL Server and Microsoft.ACE.OLEDB.12.0

I am getting the following:
Msg 7399, Level 16, State 1, Line 1 The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" reported an error. The provider did not give any information about the error. Msg 7303, Level 16, State 1, Line 1 Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
When I execute the following from a Stored Procedure in SQLServer -
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=c:\Users\v-bimoss\Source\Workspaces\External\Onesite\URLoadDB\Templates\T1-Current.xlsx', 'SELECT * FROM [Sheet1$]')
I have been through all the threads I could find on this topic - but still am having the problem
I have verified that:
The 64 bit Microsoft.ACE.OLEDB.12.0 drivers are loaded
Ad Hoc Distributed Queries is set to 1
Dynamic Parameter and Allow Inprocess are both set to 1
Changed MSSQL to run under my account
The account that is running MSSQL and the account I am using both have full access to C:\Windows\ServiceProfiles\LocalService\AppData\Local\Temp
Any help would be appreciated. -wmm
I had the same problem. After going through a number of answers installing the provider, dealing with the cryptic errors, I had a brief moment of joy when I didn't get an immediate error- instead the query never returned...
I finally got it working after wasting more than a day on it. The final fix was to change SQL Server to the Local System account to login.
Here's what I did:
Ensure Office and SQL Server have the same bit width (64-bit in my case).
Install ACE provider for 64-bit (Jet for 32-bit)
Configure server
USE [MSDB]
GO
sp_configure 'show advanced options', 1
GO
RECONFIGURE WITH OverRide
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE WITH OverRide
GO
USE [master]
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.16.0', N'AllowInProcess', 1
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.16.0', N'DynamicParameters', 1
GO
Configure SQL logon. In SQL Server Configuration Manager
Open the server Properties -> Log On -> Log on as:
Change to Built-in account - Local System
Finally, here's my query:
SELECT * FROM OPENROWSET(
'Microsoft.ACE.OLEDB.16.0'
,'Excel 12.0;Database=C:\Temp\Test.xlsx;HDR=YES'
,'SELECT * FROM [Sheet1$]')
Sadly, OPENROWSET is know to be buggy for ACE. So there are a bunch of different things to try when trying to tweak it. Adding and removing a few of the following suggestions will hopefully do it for you:
1) xlsx files need to be called with Excel 12.0 Xml;
2) HDR=YES; Tells if a header record is present. It'll crash if there is and you have numerics. Change to NO if there is no header.
3) IMEX=1; This allows for mixed numeric and text fields. In case people start mixing letters in with your numerics.
4) There can be registry problems based on ACE 12.0 vs ACE 14.0. Double check your version.
5) Lastly, weirder than weird... OPENROWSET crashes when you have comments (or don't have comments). If you have --comment, by putting a space after the dashes, -- comment it sometimes helps. So if you have comments, try removing them or rearranging them and you may get a surprise. (I know... it's really a sad state of affairs.)
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0 Xml;HDR=YES;IMEX=1;Database=c:\Users\v-bimoss\Source\Workspaces\External\Onesite\URLoadDB\Templates\T1-Current.xlsx', 'SELECT * FROM [Sheet1$]')
Anyway, since OPENROWSET can be a bit unstable, you may want to import data using a different approach with staging tables, or csv files, etc. But if you get it working, it can be quite nice.
Hope that helps :)
I would check into the steps in these two but in particular checking and changing, if you can, the user who runs SQL Server:
Cannot create an instance of OLE DB provider Microsoft.Jet.OLEDB.4.0 for linked server null
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)"
Like you, I tried everything and I was near to despair when I discovered that the sheet in the excel file must have a name, and it must be referenced with a DOLLAR sign, like [mysheet$].
So here is my openrowset statement that finally works:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.16.0', 'Excel 12.0;Database=\\svr\dir\myfile.xlsx;HDR=YES', 'SELECT * FROM [mysheet$]')
We are using SQL Server 2014 64-bit, using latest ACE provider (Version 16).

SQL Import and Export Wizard

I need to import an Excel file into a SQL Server 2012 database. It will contain around 12,000 rows every month. I know I can use the wizard to perform this task but I would like to delete the rows from the destination table if they match the ID number of the data being imported.
Would I be able to import the data into a temp table then do the match/delete all in one script via the wizard?
Should I be rather looking at another method?
Thanks
There is actually a way to query an Excel file the same way you might do to a table, and it would allow you to pretty easily do what you described. With that said, there is a little bit of preliminary legwork to get it to work.
For this explanation, I tested locally with an xlsx file (created with Excel 2013) with a single sheet Sheet1 with two columns, the first row containing the column names Field1 and Field2.
Here is what I am able to do currently.
--SELECT from Sheet1 into a local temp table
SELECT * INTO #TempTable
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;
Database=C:\Users\pwalton\Documents\test.xlsx',
[Sheet1$]);
--Clear out the original records
DELETE FROM TestImportTable
WHERE Field1 IN (SELECT Field1 FROM #TempTable)
--Insert the new ones
INSERT INTO TestImportTable
SELECT * FROM #TempTable
--Get rid of the evidence!
DROP TABLE #TempTable
Here's what I needed to to to get to this point.
I downloaded and installed 2007 Office System Driver: Data Connectivity Components.
I had to enable ad hoc queries, with the following command.
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO
I had to make sure that I was running SQL Server Management Studio as a system admin. In my case, I needed to shift right-click and Run as administrator. If you are in a more managed environment, work with your Network Administrator to get the correct rights.
That should be all you need. Again, I tested this locally, and each of the items I listed above were needed to overcome particular errors along the way.

SQL Query to return Remote Access Configuration

I'm trying to develop a script that will query a SQL DB and its instances to see if remote access is enabled. I can find lots of information on how to manually do this through the SQL Management Console and commands on how to alter the access, but my search is coming up empty on how to just confirm the current state of the config via sql query. Does anyone know how this would be achieved?
Below commands will allow you to configure the relevant sections to allow or disallow various remote sql connections. I'd like to know how to query the current state of each config itme.
exec sp_configure "remote access", <0 or 1>
exec sp_configure "remote query timeout", <number of seconds>
exec sp_configure "remote proc trans", <0 or 1>
select
*
from
master.sys.configurations
where
left(name,6) = 'remote'
order
by name
There are a few columns there you might be interested in. But for your questions, I think you want [value_in_use].
Simply call sp_configure supplying only the setting name in order to query the value:
exec sp_configure "remote access"
exec sp_configure "remote query timeout"
exec sp_configure "remote proc trans"
You can also omit both parameters to query the values of all settings.
The results will show you the name of the option, the minimum and maximum values, the current running value (what's actively in use), and the configured value (what will be used once a reconfigure command gets executed).
MSDN link.

Accessing another SQL Server from SQL Job Agent

I have a SQL Server Agent Job on "server X." This job is simple, and uses the following query to refresh a table (on server X) by clearing it, then re-populating it with data from a view (also on server X):
DELETE FROM [ClientList].[dbo].[LatestDownloadLogs]
INSERT INTO [ClientList].[dbo].[LatestDownloadLogs]
SELECT * FROM [ClientList].[dbo].[latestoverview-union]
The "LatestDownloadLogs" table is moving to "server Y," but the "latestoverview-union" view will remain on "server X".
Therefore what I need is something that looks like this:
DELETE FROM [server Y].[ClientList].[dbo].[LatestDownloadLogs]
INSERT INTO [server Y].[ClientList].[dbo].[LatestDownloadLogs]
SELECT * FROM [server X].[ClientList].[dbo].[latestoverview-union]
Of course, it's not that easy, but hopefully that illustrates what I'm trying to accomplish.
Create a linked server on server x to server y.
You could use OPENROWSET, which'll require the connection info, username & password...
But first you may need to turn on Ad Hoc Distributed Queries
EXEC sp_configure 'show advanced options', 1
reconfigure
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
reconfigure
You can then select, insert or delete
SELECT FROM
OPENROWSET (... params...)
UPDATE
OPENROWSET (... params...)
Hope this helps... good luck.

Resources