Logo 
Search:

SQL Server Articles

Submit Article
Home » Articles » SQL Server » MiscellaneousRSS Feeds

Transfer Data of Table from One Database to Another

Posted By: Easy Tutor     Category: SQL Server     Views: 4981

This article will explains you how can we transfer data of table from one database to another in SQL Server. We would make use of Cursor to transfer data of table from one database to another database.

Syntax for Transfer Data of Table from One Database to Another in SQL Server

DECLARE c1 CURSOR READ_ONLY
FORselect Col1, Col2, Col3 from SourceDBName.dbo.SourceTableName

declare @Col1 smallint --(Change Datatype respectively)
declare @Col2 varchar(50)
declare @Col3 intopen c1

FETCHNEXTFROM c1
Into @Col1, @Col2, @Col3

WHILE@@FETCH_STATUS = 0
BEGIN

--InsertInsertinto DestinationDBName.dbo.DestinationTableName
(
    Col1,
    Col2,
    Col3
)
values
(@Col1, @Col2, @Col3)

FETCHNEXTFROM c1
Into @Col1, @Col2, @Col3

ENDCLOSE c1
DEALLOCATE c1

Example for Transfer Data of Table from One Database to Another in SQL Server

DECLARE c1 CURSOR READ_ONLY
FORselect [Id],[ServiceType],[Points] from DailyFreeCodeQA.dbo.[ServiceType]

declare @Id smallintdeclare @ServiceType varchar(50)
declare @Points intopen c1

FETCHNEXTFROM c1
Into @Id, @ServiceType, @Points

WHILE@@FETCH_STATUS = 0
BEGIN

--InsertInsertinto DailyFreeCodeProduction.dbo.[ServiceType]
(
    Id,
    ServiceType,
    Points
)
values
(@Id, @ServiceType, @Points)

FETCHNEXTFROM c1
Into @Id, @ServiceType, @Points

ENDCLOSE c1
DEALLOCATE c1
  
Share: 

 
 
 

Didn't find what you were looking for? Find more on Transfer Data of Table from One Database to Another Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Transfer Data of Table from One Database to Another is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!