Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two database namely "A" and "B".
I need to copy a table(along with rows,Pk,Fk,Indexes,statistics) from database "A" to "B".



Can u help me to solve
Posted

Run this query in your server by substituting Destination,Source databases and corresponding table name:


Select * into DestinationDatabase.dbo.Table From SourceDatabse.dbo.Table

For example,

Select * into StudentDB.dbo.AddressTable From EmployeeDB.dbo.AddressTable


You need not create the table using script,this one will create table and insert data into it.
 
Share this answer
 
Comments
Sandeep Mewara 21-May-11 14:02pm    
You can use 'Improve solution' at any time and update your answer. Posting multiple answers does not help in maintaing the discussion.
sk saini 22-May-11 0:54am    
Venkyanatraj, again you are right. Take my 5.
Venkyanatraj is right. Only you just have to do one more thing after copy paste. Substitute the name of the database on first line of script before executing it. i.e.,

Substitute Use [A] with User [b]

Its working perfectly in SQL Server 2005.
 
Share this answer
 
You can right click on the required table in "A" database and select the option SCRIPT TABLE AS->CREATE TO->NEW QUERY EDITOR WINDOW , copy the script or query shown in the window and execute it inside the "B" database
 
Share this answer
 
Comments
swathi6589 21-May-11 3:12am    
I tried by above method.
Everything is fine but I wont get the table entrys.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900