Using OPENROWSET for moving data from SQL Server to SQL Server

Today I had to move some data from a staging server to a production server, but instead of doing the manual move like usual I created a SQL script using the SQL Server syntax for OPENROWSET to move the data. It is a lot like the entry I made a few weeks ago with the Excel syntax, but a little different. This script was ran on the production server in order to pull the data from the staging server (which is not a linked server).

In the script (names and passwords changed for reasons I hope you know) below, STAGING - is the name of the staging database server, user - is the sql server login, password - is the password for that login, report_db is the name of the database.

-- ParameterID is an IDENTITY so let it create its own value
INSERT INTO Report_Parameter
 SELECT ReportID,
  ParameterName,
  ParameterType,
  DefaultValue,
  ValidValueList,
  Nullable,
  AllowBlank,
  Prompt,
  UseQuery,
  CommandText,
  CommandType
 FROM
 OPENROWSET('SQLOLEDB',
 'STAGING';'user';'password',
 'SELECT *
  FROM STAGING.report_db.dbo.Report_Parameter
  WHERE ReportID = 14
  ORDER BY ParameterID')

I know I could have done a DTS package faster, but I think this SQL script will be easier to edit for one-offs (that never seem to be one-off), besides that I thought it would be better to have this script in my reusable code files.

posted on Tuesday, March 23, 2004 1:15 PM

Feedback

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

Interesting script... I know that there's also a tool that do this: http://www.sqlscripter.com/
3/23/2004 10:05 AM | Stefano Demiliani

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

Stafano - cool thanks for the tip!
3/23/2004 11:28 AM | Jason haley

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

You should not be using "Select *"
3/23/2004 3:49 PM | Enigma

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

Why do you use OPENROWSET if you can write instead following script

INSERT INTO Report_Parameter
SELECT ReportID
FROM STAGING.report_db.dbo.Report_Parameter
WHERE ReportID = 14
ORDER BY ParameterID

Because you have a linked server you can use simple statement.
4/20/2005 9:07 PM | Why OPENROWSET?

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

This is an excellent example, and it is done correctly, despite the noob comments pervious. The example shows connecting to a database on a different server with different passwords and connection information. The only change I would make would be to use each field being selected directly instead of using '*' just to make sure the fields match. Good Job.
3/29/2006 8:10 AM | Kristen Mallory

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

I need to do a similar thing.Need to archive data from the production sql sever(2000) into a new sql server say History(also 2000).
I need to transfer data from 7 tables into this new history server.

Should i use a DTS or run a query as mentioned using OPENROWSET?
Thanks
11/13/2007 10:57 PM | MG

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

Cool. I like the script. I also think that using 'select *' can be resiky, but nice script. It gave me ideas.
1/25/2008 2:35 PM | JA_Coder

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

Excelent tip. I had searched in the web a lot without usefuls results. Very thanks. It is 1:00 a.m. and you page had saved me work all the night
6/20/2008 11:53 PM | Hugo Mendoza

# re: Using OPENROWSET for moving data from SQL Server to SQL Server

An important difference between using openrowset and the syntax "SELECT ... FROM Server.Database.dbo.Table WHERE ..." across a LinkedServer is to do with where the "WHERE" clause gets executed. Using the LinkedServer approach means all the data is shipped from source server to destination server and the where clause is then executed on the destination whereas openrowset means the query INCLUDING the where clause is executed on the source server and only data selected by the where part of the query is pushed across the network to the destination server.
10/24/2008 12:14 AM | BFAndy

Post Comment

Title  
Name  
Url
Comment   
Please enter the following code into the box below to stop spammers

  
Enter Code Here *