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.
Related
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')
Data 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.
I have a problem with running a query in a linked server, I use sql sp’s for ETL process in my BI project
(For some reason I cannot use ssis).one of my queries witch has to read recently changed records and insert them in my warehouse take too long to execute and always fail with this error:
OLE DB provider 'SQLOLEDB' reported an error for linked server ‘XXX’. Execution terminated by the provider because a resource limit was reached..
But other queries run successfully. I also run following scrip in my linked server (warehouse) to increase timeout threshold.
sp_configure 'remote login timeout', 30
go
reconfigure with override
go
sp_configure 'remote query timeout', 0
go
reconfigure with override
go
Hint: I’ve used change tracking option in source tables to track updates and inserts..
I would be really thankful if someone could help me out of this.
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.
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.
I'm getting the following error when trying to open an Excel file in SQL Server 2008 r2 64-bit:
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)".
I'm using the following query:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;
HDR=NO; IMEX=1; Database=\\filepath\filename.xlsx', 'SELECT * FROM [Sheet1$]')
The funny thing is that the DBA can run it without issue. I've gone through and ran the following queries:
sp_configure 'Show Advanced Options', 1;
RECONFIGURE;
GO
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
The account that runs it looks like it has sa access. What could be causing this issue?
As Philip has said...first check the execution of xp_cmdshell. If it is not running due to permission issue then first reconfigure this option by running
SP_CONFIGURE 'XP_CMDSHELL',1
GO
RECONFIGURE
after this run following command to enable linked server permissions for InProcess capabilities for ACE driver :
USE [master]
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
GO
Now run this series of commands :
sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
if error encountered then run each command separately. And finally run import all your excel data to SQL server by running the below mentioned command :
SELECT * INTO TargetTableName FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=FilePath\fileName.xlsx;HDR=YES',
'SELECT * FROM [sheetName$]')
Remember that in case of xls you have to use Jet Driver instead of ACE. And also the TargetTableName must not be existing prior to running this query.
Happy coding :)
have you tried (as a test) copying the Excel file onto the SQL Server C:\ drive and executing the query against that path?
what happens when you go onto the server and open this path in Explorer/run dialog: \filepath\filename.xlsx?
Are you able to execute this query: exec master..xp_cmdshell 'dir '\filepath\filename.xlsx'?
This will help you determine if it's a network rights issue, or whether the account has the permissions to use distributed queries.
My hunch is that it's definitely a rights/permission issue, as the DBA can run it.
Please use forward slash instead of back slash
Select * From OPENROWSET('MICROSOFT.ACE.OLEDB.12.0','EXCEL 12.0;DATABASE=//ComputerName/ShareMyLocation/DatabaseSheet.xlsx;IMEX=1','Select * From [SMDH-View2$]')
SQL Server Management Studio. Type Services.msc in the run command to open the services window.
Search for SQL Server Service and right click it and select properties.
In the Log On Tab, select system account/or select your domain ID and Account and password.
Once it finds your login name press OK.
Now type your login’s passwords in both the fields.
Restart the services so that the new changes are applied as shown in figure below.
Now start SQL Server Management Studio and try to run the query if still not working try a system restart.
or execute the query.
USE [master] GO EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
GO