Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 moving one table form db to another dB

Author  Topic 

Pasi
Posting Yak Master

166 Posts

Posted - 2014-05-08 : 17:00:01
HI All,

Wondering if its possible to copy a table form one DB and paste to another dB? is there a tool for that? I guess copy past out of questions?
Thanks,
Pasi

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-05-08 : 17:10:10
This will grab table definition and data:
select *
into DB2..Table1
from DB1..Table1

If you need to copy just the table definition, then add WHERE 1=0.

If you want indexes, constraints, etc, then you can script those.



Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-05-08 : 17:31:27
Thanks Tara!

quote:
Originally posted by tkizer

This will grab table definition and data:
select *
into DB2..Table1
from DB1..Table1

If you need to copy just the table definition, then add WHERE 1=0.

If you want indexes, constraints, etc, then you can script those.



Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/

Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-05-08 : 17:35:13
Do I need the dots.......
Pasi

quote:
Originally posted by tkizer

This will grab table definition and data:
select *
into DB2..Table1
from DB1..Table1

If you need to copy just the table definition, then add WHERE 1=0.

If you want indexes, constraints, etc, then you can script those.



Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-05-08 : 17:37:21
The dots are due to using the 3-part naming convention. If your tables are owned by dbo, then DB1..Table1 is equivalent to DB1.dbo.Table1. You do need the dots for the remote table as you need to use the 3-part naming convention for a remote object on the same SQL instance. For a remote object on a remote SQL instance, you need the 4-part naming convention: ServerName.DatabaseName.SchemaName.ObjectName.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-05-08 : 17:53:27
ok got it thanks. didn't know it was just an writing expression or else?
Pasi
quote:
Originally posted by tkizer

The dots are due to using the 3-part naming convention. If your tables are owned by dbo, then DB1..Table1 is equivalent to DB1.dbo.Table1. You do need the dots for the remote table as you need to use the 3-part naming convention for a remote object on the same SQL instance. For a remote object on a remote SQL instance, you need the 4-part naming convention: ServerName.DatabaseName.SchemaName.ObjectName.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-05-08 : 17:55:45
If you have Sql Management Studio (SSMS) you can use the Import/Export functionality to transfer data/tables between servers/databases. Just right-click on a database and go to Tasks -> Import Data or Export Data.
Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-05-09 : 12:00:31
Thanks Lamprey! I will try that instead of restoring dB. I haven't done this before but would it ask you what table you want to export to where? cause as I accidently deleted the row on a table called user_pref, would this option good for me to restore the table back from one dB to another?

Pasi

quote:
Originally posted by Lamprey

If you have Sql Management Studio (SSMS) you can use the Import/Export functionality to transfer data/tables between servers/databases. Just right-click on a database and go to Tasks -> Import Data or Export Data.

Go to Top of Page
   

- Advertisement -