SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Inserting from another database
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

deanglen
Yak Posting Veteran

58 Posts

Posted - 05/03/2013 :  05:19:18  Show Profile  Reply with Quote
Hi

I have an empty sql table in a database (call it database1) that I want to insert from a backup (call it database2)

The SQL script I have have is this

SET IDENTITY_INSERT dbo.MobileDevice ON
INSERT INTO dbo.MobileDevice (MobileDeviceID, UserAgent, Name)
SELECT * from dbo.MobileDeviceOld
SET IDENTITY_INSERT dbo.MobileDevice OFF

This is under the situation where dbo.MobileDeviceOld exists but it does not. What I want to do is insert all the values from dbo.MobileDevice from database 2 into dbo.MobileDevice in database1

Is this possible? Never done this before.

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 05/03/2013 :  05:34:05  Show Profile  Reply with Quote
provided both databases are in same server you can use this

USE database1
GO
SET IDENTITY_INSERT dbo.MobileDevice ON
INSERT INTO dbo.MobileDevice (MobileDeviceID, UserAgent, Name)
SELECT * from database2.dbo.MobileDevice
SET IDENTITY_INSERT dbo.MobileDevice OFF


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 05/03/2013 :  05:37:07  Show Profile  Reply with Quote
If the databases are in different servers you need to setup a linked server connection between them and then use query like

USE database1
GO
SET IDENTITY_INSERT dbo.MobileDevice ON
INSERT INTO dbo.MobileDevice (MobileDeviceID, UserAgent, Name)
SELECT * from Server2.database2.dbo.MobileDevice
SET IDENTITY_INSERT dbo.MobileDevice OFF


Server2 is linkedserver connection you set in Server1 to other server

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000