Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL - transferring data to another database

Status
Not open for further replies.

puterkrazy

Programmer
Aug 6, 2003
30
0
0
US
I need to transfer data to another database, what are SQL commands to do this and what is the best way?
 
If you are using SQL server then use the databse copy or do a back up and restore of the database you are trying to move. You can also create the database and import the data from your original database to the target database.
 
sorry i was not clear at all... i need to transfer data programmatically, during runtime. what are commands to do this? i know little about load/unload, is there something better?
 
Using a simple INSERT...SELECT query would probably be easiest, depending on your situation. Can you provide more detail:

Is the DB on the same server?
Do the target tables already exist?
Do you need to scrub the data before transferring?
etc..

--James
 
The tables do not exist and the DB may not be on the same server. How do you connect to a different database?
 
If the target DB is on a different server, you just need to set it up as a linked server (look this up in BOL). Then you can refer to it using:

Code:
SELECT * FROM server.dbname.owner.table

--James
 
thank you that is helpful! how do you transfer the data then?
 
If the table exists then use a simple INSERT command:

Code:
INSERT server2.db1.dbo.t1 (col1, col2, col3)
SELECT col1, col2, col3
FROM t1

If the table doesn't exist, you can either run a CREATE TABLE command first, or use SELECT INTO:

Code:
SELECT col1, col2, col3
  INTO server2.db1.dbo.t1
FROM t1

--James
 
The code JamesLean gave you will create the table automatically.

On the database WITH the information you want to transfer, run a statement like this:

SELECT col1, col2, col3
INTO server2.db1.dbo.t1
FROM t1

(if you want to transfer the entire table, you can do this)

SELECT *
INTO server2.db1.dbo.t1
FROM t1

Change INTO server2.db1.dbo.t1 to the server.database.owner.tablename you want to create, and change FROM t1 to whatever table you're transferring the data from. SQL Server will automatically create the table specifed in the INTO server2.db1.dbo.t1 and populate it whith the data from FROM t1.

Hope this Helps!
 
I am still getting the following error:

Server: Msg 117, Level 15, State 1, Line 3
The object name 'server.db.dbo.' contains more than the maximum number of prefixes. The maximum is 2.


help please!
 
How about creating a DTS package (via the DTS wizard). Simply select your source and destination and then select all the tables you want to copy over. If you want the "SQL Select" statement, in the "Select Source Tables and Views" screen click the "Transform" button and in the "Column Mappings and Transformations" window click "Edit SQL", this will give you the exact SQL statement to copy your tables to another location, you can simply copy-paste it.

If you need the VBS code, go to the "Save, schedule, and replicate package" screen (screen after "Select Source Tables and Views") and check "Save DTS Package" and then check "Visual Basic", this will ask you for a file name and location to save the VBS code at the end of the wizard screen.

I hope this helps...

Arles
 
Are both databases on the same server? If so, drop the 'server.' from the 'server.db.dbo.tablename'

Hope this helps!

 
Arles thank you, that was a good idea. Unfortunately the SQL code creates the create table without the server specified! Ugh!

The databases are on different servers...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top