Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Frnds,
I have 2 database with one or more tables.I need to move one table of 1st datbase to another 2nd database table by using SQL query
Posted

if both tables structures same
SQL
Insert into database2.dbo.Tbl1 select * from database1.dbo.Tbl1

Happy Coding!
:)
 
Share this answer
 
Comments
itsureshuk 8-Nov-12 0:14am    
If i need to copy only three columns datas from one table to another means
SQL
declare @fieldsName varchar(max)
declare @qry varchar(max)
set @fieldsName = 'field1, field2, field3'
set @qry = 'insert into database2.dbo.tablename(' + @fieldsName + ') select ' + @fieldsName + ' from database1.dbo.tablename'
exec (@qry)
 
Share this answer
 
select * into database2.tableName from database1.tableName;

database2.tablename (Target data table)
database1.tablename (source data table)
 
Share this answer
 
Comments
itsureshuk 8-Nov-12 0:14am    
If i need to copy only three columns datas from one table to another means
StackQ 8-Nov-12 0:25am    
SELECT 1stColumnName,2ndColumnName,3rdColumnName into database2.tableName from database1.tableName;
itsureshuk 8-Nov-12 0:35am    
thnks

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