How to copy table from one Data Base to another? - sql

I want to copy some tables from my DB1 to my DB2. Tables in DB1 are same as tables on DB2 but data in the tables are different. I want to copy tables from DB1 to DB2 but to keep old tables and data on DB2. How I can do this with Microsoft SQL Server Management Studio? I tried to right click and do the export but before I have to click on Finish button looks like that will just copt all data from DB1 to DB2 and I do not want that. If anyone can help with this please let me know. Thank you.

You can export the tables from DB1 with another name to DB2 if you don't want to modify them. In the export wizard just change the name of the destination table.

So you want to merge the schema AND data from DB1 into DB2?
You should list out the exact requirements, the question is still vague even with that info.
What data do you want to keep, what is ok to blow out?
What schema do you want to keep, are you archiving the old tables? Changing table names?
If you are literally trying to merge db1 into db2 your issue is going to be in managing the relationship ids which will be getting reassigned since DB2 could already be using IDs that are present in DB1.

If you want to keep old data in the destination table (or juste update it), so you might use a Merge:
MERGE INTO db2.dbo.table1 B
USING (
SELECT *
FROM db1.dbo.table1) A
ON (A.Column1 = B.Column1 )
WHEN MATCHED THEN
-- update
WHEN NOT MATCHED THEN
-- insert new rows

USE db2;
CREATE TABLE table2 LIKE db1.table1;
INSERT INTO table2
SELECT * FROM db1.table1;
This is also a way to copy a table with its records to another database.

Related

Nothing from a table shows up in SQL

I have several table created by recently i can get anything to come from one of them.
The table is called REP and i'm currently using Live SQL from Oracle as i'm quite new to SQL
The select command i'm trying to use.:
select * from REP
I go to my schema and the table is definitely there. No commands for this table work.:
If anyone could help i'd very much appreciate it. Don't be too hard on me i'm quite new to SQL.
Your table is empty. First insert the values into this table via
INSERT INTO REP VALUES(...)
and
then use your query Select * from rep
As in your image, the table is there thats the ddl view meaning table's structure not data i.e. you have only columns but no data.

Importing Data From One database to another

I wanted to know that, Suppose I have a table in one database with say 1000 records and I have similar table in another database with say 500 records.
Now my question is If I will try to import data from say DB1.Tbl1 to DB2.Tbl1, then what will happen? Is there any possibilities of duplicacy of the data?
I wanted the records of DB1.Tbl1 to copy into DB2.Tbl1 table. Please clear my confusion.
If you have same data in both tables you can first truncate 2nd table after that you can import data from 1st table by Insert Command or "import data" task
Try this
INSERT INTO DB1.dbo.Tbl1 SELECT * FROM DB2.dbo.Tbl2
This just moves the data. If you want to move the table definition, you have to do something else.
Note that, SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.
If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.
Try this:
We can copy all columns from one table to another, existing table:
INSERT INTO table2
SELECT * FROM table1;
Or we can copy only the columns we want to into another, existing table:
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

Comparing the data of two tables in the same database in sqlserver

I need to compare the two table data with in one database.match the data using some columns form table.
Stored this extra rows data into another table called "relationaldata".
while I am searched ,found some solutin.
But it's not working to me
http://weblogs.sqlteam.com/jeffs/archive/2004/11/10/2737.aspx
can any one help how to do this.
How compare two table data with in one database using redgate(Tool)?
Red Gate SQL Data Compare lets you map together two tables in the same database, provided the columns are compatible datatypes. You just put the same database in the source and target, then go to the Object Mapping tab, unmap the two tables, and map them together.
Data Compare used to use UNION ALL, but it was filling up tempdb, which is what will happen if the table has a high row count. It does all the "joins" on local hard disk now using a data cache.
I think you can use Except clause in sql server
INSERT INTO tableC
(
Col1
, col2
, col3
)
select Col1,col2,col3from tableA
Except
select Col1,col2,col3 from tableB
Please refer for more information
http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/
Hope this helps

Create table with Select * into with different collation

I have a database (DB1) with one collation and many tables.
I've got another database (DB2) with a different collation (on another server).
I want to create copies of the tables from DB1 on DB2, but with the collation of DB2.
The tables have many columns and there are a lot of tables so I don't want to have to stick COLLATE .... on each column. I'd much rather be able to do SELECT * INTO ... FROM ....
How can I change the collation of the columns of the new table without specifying them all individually?
I'm running MS SQL 2005.
as what I have understood this problem .. solution is -
you can use DB link .... try to link all the desired tables from DB1 and create synonym in DB2
(obviously you have to give synonym name other then the table name .. because tables with same name will be there also in DB2)
now try to create new tables with union to DB2 tables (in other word you can say I am saying to merge the tables)

merge msaccess tables

How can i merge two identical tables of two msaccess identical db? For eg:
db1..table1
ID Name
1 Sanjana
2 Parul
3 Rohan
db2...table1
ID Name
1 Sarika
2 Deepak
I want to append the values of second table into first as follows:
ID Name
1 Sanjana
2 Parul
3 Rohan
4 Sarika
5 Deepak
The datatype for the ID field appears to be an autonumber. As such you can do the following:
INSERT INTO db1...table1
SELECT Name FROM db2...table1
You can use an append query:
INSERT INTO Table1 ( FName ) IN 'c:\docs\ltd.mdb'
SELECT A.FName
FROM A;
OK, heres an approach more suited to a beginner making use of the gui.
Backup both databases and store them away somewhere safe.
Do a compact and repair from the tools menu on both databases
Create a linked table in db1 pointing to the table in db2
To do this right click on some white space in the table view of database window and choose link table... Follow the wizard to select db2 and then select table1.
Use an append query to append the data from the linked table1 into the db1.table1
Click into the queries view of Access, create a new query in design view, change its type to Append (right click in free space where the tables appear and go to type->append) Then choose db1.table1 when prompted as the table to append to. Now add the linked table1 into the query, select the fields from which you want to take data (in the example it would just be Name). Note you do not want to take the id field across as this will need to be updated to follow on from where db1.table1 left off, im assuming this is set to autonumber.
Delete the linked table from db1
Im not 100% certain the sort order will be retained from db2.table1 when its appended to db1.table1 as in your example. In most database designs this wont be important but if it is someone else may be able to shed light - i imagine that if the ID field in both tables is also the primary key it will.
Well since it is access, you have two ways. the first is mentioned by LSFR Consulting
and the second would be to use the import wizard and tell the import to ignore the primary key column. That would merge the data from db2 into DB1 without having a primary key collision.
If this is a one time operation simple copy-n-paste will work.
Open both databases in MS Access. Open both tables. Select values to copy (right-click on Column header and Ctrl+C).
Proceed to target table. Make Name field selected in the last row (new record). For this purpose mouse over Name column left edge (cursor becomes a plus sign) and click to select the cell. Ctrl+V. Done.