Oracle update table with the value from another database - sql

In one db i have table PRODUCTS with columns NAME and TECHNICAL_NAME.
In second database I have table TEMP_PRODUCTS with columns NAME (that corresponds to column NAME of the table PRODUCTS from the 1st db) and TECHNICAL_NAME that is null and that should be updated with the corresponding TECHNICAL_NAME values from 1st db.
So I should do UPDATE table TEMP_PRODUCTS by using JOIN with columns NAME?
I would like to avoid solution with exporting table from 1st bd and importing it to the 2nd db.
How can I do this?

By Creating the DB Link only we can access the one DB objects from another DB.
CREATE DATABASE LINK db1_link
CONNECT TO <User Name> IDENTIFIED BY <pwd>
USING 'db2';
-- db2 means Service Name for exp products db
Then Update Statement
UPDATE temp_products tp
SET technical_name=
(SELECT technical_name FROM products#db1_link p
WHERE tp.name = p.name)

Assuming:
both NAME columns are UNIQUE;
you have a database link in db2 pointing to db1.
UPDATE temp_products tp
SET technical_name=
(SELECT technical_name FROM products#db1 p
WHERE tp=name=p.name)

You need to execute the below query in db2 ,where temp_product table lies,and db link db1 on the same database db2,to connect db1.
MERGE temp_products tp
USING products#db1 pp
ON(tp.name = pp.name)
WHEN MATCHED THEN
UPDATE SET tp.technical_name = pp.technical_name;
You need to create a db link in database db2,so that you can connect to db1 ,to acces product table.
Please find the syntax for creating db link
CREATE [PUBLIC] DATABASE LINK <link_name>
CONNECT TO <user_name>
IDENTIFIED BY <password>
USING '<service_name>';

Related

Importing sql table (t1) from one db (db1)to another (db2) (t2)and then use t2 to update values of a table in db2

I need a query to import data of a table into a different table and different database. Then using the new table I need to update another table in same database
As you tagged SSMS, I'm assuming you're using SQL Server. This answer may apply to other databases, but you'd need to check.
How to refer to other databases
Assuming your database is on the same server, and the table is in the 'dbo' schema, then you can refer to the other database/table as follows
SELECT *
FROM [db1].[dbo].[t1]
If it's on a different server (but has been set up as a linked server) you can do something similar
SELECT *
FROM [servername].[db1].[dbo].[t1]
Making a copy of the data
To save it as a new table within your current database, you could either create a copy of the table's structure in the current database and use an INSERT INTO command, or instead let SQL Server make the table for you by using the 'INTO' clause in the SELECT e.g.,
SELECT *
INTO [db2].[dbo].[t2] -- Or just simply [t2] if you're running this from that database and are happy with [dbo]
FROM [db1].[dbo].[t1]
The above copies the data from the t1 table in db1 to a new table t2 in db2.
You can then use the data in t2 as you normally would use a table.
If you're running this from within d1, you will always need to refer to the tables in d2 properly (and/or alias them) as below.
UPDATE t3
SET xfield = t2.zfield
FROM [db2].[dbo].[t3] AS t3
INNER JOIN [db2].[dbo].[t2] AS t2 ON t3.id = t2.id

Trigger to update table column when data is inserted in another table from a different database

I have a database table named vendor_invoice which contains columns invoiceNo and paymentref in database Interface. I want to update the paymentref column once data has been inserted into another database table named APTCR which is located in another database, JBDAT. Note that table APTCR has both the paymentref and invoiceno columns.
It would be possible if you use like oracle dblink or db2 federation or change data capture software.
If you use dblink or federation. You can use trigger :
create or replace trigger aaa
begin
...
end;
Otherwise, IBM IDR, Oracle Data Replication would be a good solution

How to pass list of IDs from one Oracle schema to another?

I use the Oracle SQL developer to query the Oracle database
So, my simplified script is as follows:
alter session set current_schema=schema1;
select id from table1
alter session set current_schema=schema2;
select * from table2 where remote_id in (<the list from the 1st query in schema1>)
Currently I copy the list from one schema to another manually. How to automate passing the list?
Fully qualified database object references in Oracle are SCHEMANAME.OBJECTNAME so regardless of which schema is your current schema, you can reference objects in other schemas like so:
Select *
from schema2.table2
where remote_id in (select id from schema1.table1);

Microsoft SQL Server 2005: Create alias for database

How can I create alias name to my database in SQL Server 2005?
For example: DB1 and its alias DB2, it's same DB, but with two names.
Or can I do replication, mirroring, syncing or anything other inside server from one DB to another?
You can do replication from one database to another database on the same machine. You can also copy data directly without having to create an alias. For instance if you had a table named Users in DB2 and a Users table in DB1 and they are the same schema you could easily just do
INSERT INTO DB1..Users
select * from DB2..Users
Now, a synonym would allow you to use a table from DB2 as if it was a table in DB1 so for instance if you have a table named Products in DB2 you could do
use DB1
GO
CREATE SYNONYM [dbo].[Products] FOR [DB2].[dbo].[Products]
GO
-- Now the following would give you the same result
select * from DB2..Products
select * from Products
For more information on synonyms see here

Updating columns values from another table SQL

I want to update my table from another table in another database.I have two table that has two same columns.There are ID and iexp column.What i want is update every row from k_monster table to my database's k_monster table but there are other columns such as iHP iMP so i want to just update iExp column.what do you suggest?
Assuming Target_Database is the database where the table is that you want to update and Source_Database is the database where the table is you are using to update from.
Your query should look something like this.....
USE [Target_Database]
GO
UPDATE t
SET t.iexp = S.iexp
FROM K_monster t
INNER JOIN [Source_Database].[Schema].[K_monster] S
ON t.ID = S.ID
GO
Please check this link similar to this type of question:
Additionally I would suggest you to search before you ask any questions.
UPDATE record in one database with values from another in SQL Server 2008?
This link has similar answer to your question.
More links:
Update database table from one SQL Server database table to another?
https://dba.stackexchange.com/questions/30228/how-to-update-one-database-from-another
https://dba.stackexchange.com/questions/58371/sql-update-column-with-data-from-another-table
With Regards