Inserting a single row from different databases - sql

To start off we have two databases. One called TOk and the other called test2. test2 is a backup of TOk. A situation occurred in which a single row was deleted accidentally from the TOk database in the hazards table. I just want to know if there is a way to copy that record single row to another database. HazID is the unique identifier in that particular table so would something like. Both databases reside on the same instance.
INSERT INTO Tok.hazard.dbo *
Select * From Test2.hazard.dbo
Where HazID=(the specific ID of the missing record)
Is this close?
Basically expect the record to be copied back into the live database.

Related

Batch insert to database with skipping duplicates found in database

I'd like to save to the database a list of objects using Spring. In my controller I receive a list of strings, say these are cities. I filter out duplicated and have a unique Set of cities. How do I insert that set the database when I have a unique constraint on the city column?
I see three options here:
Before any insert I check whether there is already such a record, if there is none, I persist the domain object.
I try to persist the object domain object without checking whether it exists in the database or not. When I encounter an SQL Exception, I try to save another domain object.
I retrieve all the objects from the database (the list of records may definitely be larger than 100 thousand), then I create a set of unique (absent in database) domain objects and then save these to the database.
What would be the option to go? Is there (hopefully) a better option?
You could create a global temporary table owned by the oracle user that does the inserts with the same structure as the destination table. The following pseudo code shoud give you an idea. The least fuss is have it created with ON COMMIT DELETE ROWS but sometimes it is easier to use ON COMMIT PRESERVE ROWS and start off every session with
DELETE FROM <your global temporary table>;
Then insert all your data from Spring into this table. I will assume that there are no duplicate ID's or duplicate cities with the same ID in the destination table.
INSERT INTO <your destination table> SELECT city_id, city_name, third_field
FROM <your global temporary table>
WHERE <your global temporary table>.city_name
NOT IN (SELECT <your destination table>.city_name
FROM <your destination table>);
If you have duplicate city names with different ID you can just add another NOT IN clause.

Data Agent - SELECT from one table and insert into another

Is there any type of product where I can write a SQL statement to select from one table and then insert into another database (The other database is out in the cloud). Also, it needs to be able to check to see if that record exists and then update the row if anything has changed. Then it will need to run every 10-30 minutes to check to see what has changed or if new records have been added.
The source database and the ending database have a different schema (if that matters?) I've been looking, but it seams that only products out there are ones that will just copy one table and insert into a table with the same schema.

Import Export Wizard keep Identity, No Alter Table permissions

Basically, I am transferring data from one database table(A) to another database table(B).
Both databases have data except A gets updated daily and B needs to be updated with the current data in A. I would like to keep the identity column the same in both.
I try to run the wizard with delete previous data and the keep identity checked, but I get an error saying I don't have permission to alter table, so I am thinking that delete previous data truncates the table correct?
I then tried to use append table, but that complains about overwriting row with same identity value. Is there a way to ignore previous entries and only insert the new entries?
Sure, in your SELECT statement where you get data from table A, just select rows where A.ID is not equal to B.ID.
This functionality might work better with a trigger on table A or using transactional replication.

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...

mysql: how do i make a copy of the entire database?

how do i make an exact duplicate copy of a database in mysql?
create database test1 from test ???????
if this is not possible, how do i copy a table from one database into another"?
One way to do it is taking a dump from the source db and importing it into another db, like this:
mysqldump src_db > src.sql
mysql dest_db < src.sql
You can't per say. You need to create the new database, create the tables and then do inserts i.e. insert into INSERT INTO newdatabase.table1 SELECT * FROM olddatabase.table1;
Alternately, you could do a full database dump to a sql file and then dump this file into a new database.
A third option (easiest really if you have it) is PhpMyAdmin's copy database function. It creates the new database, the tables and inserts the data all automatically.
mysqlhotcopy db_name /path/to/new_directory
Keep in mind that the users will be blocked from updating tables while they are being copied, since mysqlhotcopy locks them.
I don't know if mysqlhotcopy locks them all at once or one at a time. If it locks them all at once, then all users will be blocked until the copy completes.
If it locks them one at a time while users are modifying them, you have a risk of the tables getting out of sync with each other. Like this:
mysqlhotcopy locks table a, and begins copy of table a
client attaches to database and tries to update tables a and c. It is temporarily blocked because of lock on table a.
mysqlhotcopy completes copy, unlocks table a, locks table b, and begins copy of table b
client makes related updates to tables a and c.
mysqlhotcopy completes copy, unlocks table b, locks table c, and copies table c
The problem is that the copied version of table a is from before the client's modification, and the version of table c is from after the client's modification, which puts the backup
in an inconsistent state.
Update
I just looked at mysqlhotcopy (MySQL 4.1.9). It locks all the tables at once.