7

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:

  1. Delete all records from the [Files] table on Remoteserver
  2. Insert data that comes from the stored procedure:

    INSERT [Remoteserver].[dbo].[files] (subdirectory,depth,isfile)
       EXEC master.sys.xp_dirtree '\\Fileserver\DBBackup',1,1;
    
  3. 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.

8
  • "unable" - what it mean? Commented Jan 9, 2015 at 5:46
  • 1
    can you include the error message you're getting? it looks to me like your table name is missing a part. To be a valid remote query it should have four parts: Remoteserver.dbname.dbo.files for example Commented Jan 9, 2015 at 5:59
  • To be honest i am not quite sure about the right syntax to be used. I tried some variations of the openquery but i am sure thay are not right cause they didnt work :)...anyways the remote table name is as follows:[Remoteserver].[dbo].[files]....To clarify i dont need to pull the data into an existing table on the localserver...i Just need to display it on a web app so everything that needs to happen should happen remotely...the only thing that happens locally is the openquery that displays the data from the remoteserver 'files' table Commented Jan 9, 2015 at 6:06
  • show us what you've tried so far? Commented Jan 9, 2015 at 6:08
  • INSERT OPENQUERY([Remoteserver], ' SELECT id,subdirectory,depth,isfile FROM [Remoteserver].[dbo].[files] ' ) VALUES ('EXEC master.sys.xp_dirtree ''\\fileserver\DBBackup'',1,1') Commented Jan 9, 2015 at 6:10

1 Answer 1

7

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Andrey! the first query worked...I have a question about the other 2 methods you mentioned...The localserver and remoteserver are in 2 different networks separated by a firewall and also the fileserver exists in the same network as the remoteserver so if i choose to use either the 2nd or the third method would it be the localserver that is executing the 'dirtree' command against the fileserver?
not at all. if you are going to use OPENQUERY it might be any other server that is configured as linked server, but this may require the MSDTC to be enabled on each server (check this stackoverflow.com/questions/27726567/…)
thank you for the link...I have a followup question related to the above so asking it here.... I have 3 commands in my stored procedure - first one deletes all records from a table , second inserts new records and third pull those records but at the moment when the query runs the delete works but i dont get the records from the 3rd statement which is the "select" ....I am unable to post the code here because it is too long for the comment block.
it's bad. for now I can't help you without any information about the problem details. and for sure discussing your new problem here is definitely off-topic... you can try to figure it out or you could ask a new question...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.