Update with Data from One to another Database - sql

I have two databases: db1 is server, db2 is local.
db2 I updated from the db1 database.
And added 4000 new records to db2.
And I want to update db1 (server) from db2 (local), but some new records have been added to db1 (server).
Now when I want to do Restore Database, some new records from db1 (server) will disappear.
How to make it so that when I update db1 (server) the added some records are not lost and
new 4000 data added from db2 (local)? Can anyone tell me a way or a tool to compare the data?

You need to be able to read/load the data from the local database to your server database.
This can be done via Linked Servers - if the servers are linked you can query data from the db2 from the context of db1.
It will looks like:
SELECT *
FROM [server].[db2].[schema].[table];
If this is not possible I am guessing you have another way to move the data there (via BCP command or maybe just script the table and the data using SSMS from db2 and create it in db1 (4000 rows is not much and you should be OK to just copy the generated SQL).
Now, having the data in one place, you must have a way to identify which rows to insert.
This can be done easily like this:
SELECT *
FROM db2Table
EXCEPT
SELECT *
FROM db1Table
So, if there is row in the db2Table that is not found in the db1Table it will be returned. Then, just insert these rows.
Please, note that EXCEPT with * is comparing all columns. So, if you have a column like ID INT IDENTITY(1,1) you will need to exclude it from the SELECT list.
Also, if you have a way to map the data between the two tables by ID or GUID (a lot of folks are using UNIQUEIDENTIFIER to synch data), you make perform more complex operations. For example:
If the GUID record is missing in the db1 table - insert it
if the GUID record is found in the db1 table - update it

Related

How to copy data from select statement to another database table?

In sql server, I have two servers, each with a database. In the first server (sql server 2005) I wrote select statements which then show data whose column names then match with the tables in the 2nd server (the schema is not the same). This is basically a data migration from 2 servers where the schema is new in the newer server.
But now how can I take the data from the select statement and basically insert that into the new table? Also the new table has auto increment on the ID column, but I want to keep the ID's from the old database because they are used in multiple tables.
Thanks
This is pretty straightforward, but has some fiddly bits that most people don't often use.
The simplest way to migrate the data to the new server would be to create a Linked Server using the instructions at this link.
Once you have the linked server configured (and, for this example, I am assuming that you are creating a link on the new server to point to the old server), I would personally make the SELECT queries into Views on the old server.
Then, on the new server, you would create a query sequence like this:
SET IDENTITY_INSERT MyNewTable1 ON
INSERT INTO MyNewTable1 [Optional field list here]
SELECT [Field List | *]
FROM MyOldServer.MyOldDB.dbo.MyViewForNewTable1
SET IDENTITY_INSERT MyNewTable1 OFF
SET IDENTITY_INSERT MyNewTable2 ON
INSERT INTO MyNewTable2 [Optional field list here]
SELECT [Field List | *]
FROM MyOldServer.MyOldDB.dbo.MyViewForNewTable2
SET IDENTITY_INSERT MyNewTable2 OFF
...
Et Voila. Your data is transferred, with the same Identity values.
To insert from server A to server B, you need a Linked Server.
To keep Identity values the same, you need IDENTITY_INSERT turned on.

Updating the content of a database with the content of another database

im new to SQL and can't figure out why my sql script isn't working.
I've two databases, and my task is to update a column of a specific table with the content of the same table in the other database if the conditions are met. The tables and columns of both databases have the same names, just party different content. I already looked through a lot of similar questions, but couldn't make it work / figure out what i did wrong.
UPDATE TABLE1
SET COLUMN_1 = Database2.TABLE1.COLUMN_1
WHERE Database2.TABLE1.COLUMN_2 LIKE '%DIN276%';
(Im running the query on the first database)
PostgreSQL database does not support cross-database queries.
You must create in your Database1 a foreighn data wrapper for TABLE1 from Database2, then you can perform queries with your TABLE1 in Database1 together with data from TABLE1 of Database2.

Deleting objects from SQL Server

In SQL Server Database Engine I have a table named Table A.
I deleted the table using graphical interface, but when I wanted to create a table with same name, the error shows
The object already exists
What is the remedy of this situation?
The following steps should help you track down what is going on and help you create your table:
Right-click on your database and select refresh
Verify that your table does not exist under this database.
If you table is
not shown here, then very likely your table is displayed under the
master database.
To create a table in your selected database,
first select the database and then run your query.
A better
option for number 4, just to be sure you are specifying the correct
database is to run the command use dbname; (where dbname is
the name of your database). Do this on the line above your create table code.

SQL Server bulk insert for large data set

I have 1 million rows of data in a file, I want to insert all the records into SQL Server. While inserting I am doing some comparison with existing data on the server, if the comparison satisfied I will update the existing records in the server or else I will insert the record from the file.
I'm currently doing this by looping from C#, which consume more than 3 hours to complete the work. Can anyone suggest idea to improve the performance?
Thanks,
Xavier.
Check if your database in Full or Simple recovery mode:
SELECT recovery_model_desc
FROM sys.databases
WHERE name = 'MyDataBase';
If database is SIMPLE recovery mode you can create a staging table right there. If it is in Full mode then better create Staging table in separate database with Simple model.
Use any BulkInsert operation/tool (for instance BCP, as already suggested)
Insert only those data from your staging table, which do not exist in your target table. (hope you know how to do it)

Can I do a INSERT using select from another DB with the same table name

I have a person table (sqlServer 2008r2) and are using .vbs and .bat files to sync some data from a table called person in database A to database B.
Note - The DB names are different but the table names are the same. Because the peson table has 137 fields I am looking for a way to write both an INSERT and an UPDATE statement. How can I do thsi without listing 137 fields?
At the moment I connect to database A and populate a recordSet called RS with the people table records.
Then I loop through RS and query the people table in database B
If found I update people table in database B with the people table record from database A
If not found I insert the people record from database A into the people table on database B
Now this is nice and easy but since it has 137 fields I do not want to write a MASSIVE update and insert statements. DO I have another option such as:
The table structures are identical between people on database A and people on database B but the recordset obtained in step 1 above is using one one DB connection and the query in step 2 is using a separate DB connection to a different DB instance on a different server.
Help.
You can do that by using a fully qualified name.
insert into DATABASE1.dbo.Person (columns)
select (columns) from DATABASE2.dbo.Person
Also there are others way to pump data from a database for another
You can take a look on your MSSMS it got a tool for that kind of thing.
Select one database and right button mouse to open the import/export wizard.
You can also take a look on replication, bulk copy, service broker, etc...