I have a linked server Remoteserver containing a table that holds file and folder names from a dir
When I am on the remote server I can run a built in procedure (xp_dirtree) and populate the 'files' table but what i need to do is to run a query from the local SQL server that does this:
Delete all records from the [Files] table on Remoteserver
Insert data that comes from the stored procedure:
INSERT [Remoteserver].[dbo].[files] (subdirectory,depth,isfile)
EXEC master.sys.xp_dirtree '\\Fileserver\DBBackup',1,1;
Select the 'subdirectory' column
I tried some things using openquery and i am able to select existing records but unable to do the insert.
Any help is appreciated.
Try this
INSERT INTO OPENQUERY([Remoteserver]
,'SELECT subdirectory, depth, [file] FROM [RemoteDB].[dbo].[files]')
EXEC master.sys.xp_dirtree '\\fileserver\DBBackup', 1, 1;
OR
INSERT INTO OPENQUERY([Remoteserver]
,'SELECT subdirectory,depth, [file] FROM [RemoteDB].[dbo].[files]')
select * from OPENQUERY([another_server_name], 'master.sys.xp_dirtree ''\\fileserver\DBBackup\temp'', 1, 1');
But in general you do not need to use OPENQUERY at all if Fileserver and Remoteserver are accessible from the local machine.
INSERT INTO [Remoteserver].[RemoteDB].[dbo].[files] (subdirectory, depth, isfile)
EXEC master.sys.xp_dirtree '\\Fileserver\DBBackup',1,1;
Related
I have three SQL Servers A, B and C. I am trying to run a same query like select ##servername from A server and run the same query in B and C from Server A. I am loading this results in a server A table. Please let me know how to accomplish this one.
You could run the inserts from the two remote servers using OPENQUERY which essentially runs the select locally on the remote server to be able to use the proper local indexes etc.
insert into localTableOnA (col1,col2,...)
select col1,col2,... from [dbname].dbo.[tablename] --on ServerA
insert into localTableOnA (col1,col2,...)
select col1,col2,... from openquery([ServerB],'select col1,col2,... from [dbname].dbo.[tablename]')
insert into localTableOnA (col1,col2,...)
select col1,col2,... from openquery([ServerC],'select col1,col2,... from [dbname].dbo.[tablename]')
You can make using linked servers.
1- WITHIN A instance
INSERT INTO [DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [B].[DatabaseName].[SchemaName].[TableName];
INSERT INTO [DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [C].[DatabaseName].[SchemaName].[TableName];
2- WITHIN A instance (Dynamic SQL)
EXEC ('INSERT INTO [A].[DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [DatabaseName].[SchemaName].[TableName]') AT [B];
EXEC ('INSERT INTO [A].[DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [DatabaseName].[SchemaName].[TableName]') AT [C];
3- WITHIN Other instance
INSERT INTO [A].[DatabaseName].[SchemaName].[TableName] (...) SELECT ... FROM [DatabaseName].[SchemaName].[TableName];
how can i insert from SQL Server to a SQLBase Database using linked Server?
Here are various examples of the correct syntax for SQLServer to SQLBase assuming your LinkedServer is called 'ISLANDLINK' . Note the two dots.
or Go here for the full script and explanation : SQLServer to SQLBase via LinkedServer
or Go here for another example: SQLServer to SQLBase via LinkedServer (more)
--Select:
SELECT * FROM OPENQUERY( ISLANDLINK, 'Select * from SYSADM.BUDGET where DEPT_ID = ''MIS'' ')
--Update:
UPDATE [ISLANDLINK]..[SYSADM].[BUDGET]
SET [BGT_YEAR] = 2016
WHERE DEPT_ID = 'MIS'
GO
--Insert:
INSERT INTO [ISLANDLINK]..[SYSADM].[EMPLOYEE]
([EMPLOYEE_ID]
,[LAST_NAME]) VALUES (99 ,'PLUMAS' )
GO
--Delete:
DELETE FROM [ISLANDLINK]..[SYSADM].[EMPLOYEE]
WHERE [LAST_NAME] = 'PLUMAS'
I'm trying to create historic data for my company; previously i asked how to copy a table with a timestamp into the same mssql database. realistically, it dawned on me that what i really need to do is the following
1) log onto my local mssql box
2) run a query of a view (select * from view_whatever)
3) copy the results of the query and add a timestamp into another database, hosted using azure SQL
if i were doing this from the same database to another table, i'd something like this:
DECLARE #copyDate DATETIME2 = CURRENT_TIMESTAMP
INSERT INTO Hst_Opportunities
SELECT OppName, Oppvalue, #copyDate AS copyDate
FROM dbo.opportunities
the from part is what im struggling with - how do i copy cross server and database when the source database is MSSQL and the target is Azure SQL?
Based on my test, it appears you could create a linked server on your local box and insert the data using four part name:
EXEC master.dbo.sp_addlinkedserver #server = N'SQLAZURE', #srvproduct=N'sqlazure', #provider=N'SQLNCLI', #datasrc=N'NAME.database.windows.net', #catalog=N'TestDb'
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'SQLAZURE',#useself=N'False',#locallogin=NULL,#rmtuser=N'USERNAME',#rmtpassword='Password'
GO
DECLARE #copyDate DATETIME = CURRENT_TIMESTAMP;
INSERT INTO [SQLAZURE].[TestDb].[Schema].[Table]
SELECT TOP (10)
[Column1]
,[Column2]
,#copyDate AS copyDate
FROM [Localdb].[Schema].[View];
I have SQL Server 2014 and was trying to find out if there's a way to bulk insert data into a table on a remote server like below.
SELECT * FROM OPENQUERY([REMOTESERVER],
'BULK INSERT [DBNAME].[dbo].[demo] FROM ''\\Share\data\demo.dat''
WITH (DATAFILETYPE = ''widenative'')');
I am trying to insert some data from my local table into linkedserver via sql server. this is what i am doing but it keeps on throwing syntax error.
TRY 1
EXEC(
'INSERT into test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)
SELECT * FROM rdata.dbo.testoperation'
)AT REDSHIFT64
TRY 2
EXEC(
'INSERT into test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)'
)AT REDSHIFT64
SELECT * FROM rdata.dbo.testoperation
Both fails.
Any thoughts where i am going wrong?
testoperation is your local table, and since your query runs on the remote server, the table does not exist.
Why don't you try inserting directly to the remote table:
INSERT into REDSHIFT64.test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)
SELECT * FROM rdata.dbo.testoperation