PostgreSQL, Update from Insert into sql - sql

I have 2 db, I have to update a table called "my_table" from db 1 to db 2.
Is there a way to dump the table from db1 and use that sql to update the table on the db2?
All I have is a sql file with INSERT INTO but I need an UPDATE
How could I do it?

Using DB link: dblink executes a query (usually a SELECT, but it can be any SQL statement that returns rows) in a remote database.
you can refer this official link(https://www.postgresql.org/docs/current/contrib-dblink-function.html) for DB link creation

Related

How to update oracle table with long string in where clause

I am using bulk copy to insert data from datatable(got data from oracle database) to sql table. So that is good and I do not have any problem whith that. So after this job when data inserted correctly I am trying to update a field of oracle database table with key of above datatable. the schema to my approach shows below.
update table1 set column1=1 where id in ( all keys of above datatable)
It is not working and oracle do not run that because string literal too long.
how to can I solve that? I do not want to create a temp table in oracle because this service working all time.
I'd consider using a subquery instead, e.g.
update table1 set
column1 = 1
where id in (select key
from above_datatable
)

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

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

Copy tables from one server to another in DB2

SELECT *
FROM table1 X, table2 C, table3 M, table4 XSDT
WHERE X.CATID= C.CATID
AND M.MEMID= X.MEMID
AND XSDT.SHIPDISC= X.SHIPDISC;
Say I want to run this query on the HOST db (external) and get its data and copy it to a local DB2 database.
Is there a way to do so in DB2?
I know teradata has fastload... but I'm not sure about db2 or how I would go about doing so.
Please keep in mind I do not have dba-level privileges.
Solution to this: http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=%2Fcom.ibm.db2.udb.admin.doc%2Fdoc%2Fr0002079.htm
If you want to do this with SQL, then you would use something like the following SQL:
create table schema2.table1;
insert into schema2.table1
select * from schema1.table1;
Since you're joining tables, you would have to define the local table in your CREATE TABLE SQL and list the columns in your INSERT as well as your SELECT.
You can do a DB2 backup of the tables, and restore them to your local schema.
You can do a DB2 export of the tables, and use DB2 import to create them on your local schema.
You can use the DB2 db2move utility.

Using SQL Server DTS Package to Conditionally Insert / Update Rows in Destination Table

I want to create a DTS Package to pull data from an Oracle table into a SQL2K
table. How can I insert rows that are not already in the SQL2K table and
update rows that already exist in the SQL2K table?
I guess I could truncate and repopulate the entire table or create a
temporary table and then do updates/inserts from the temp table into the
destination table.
Is there any easier way using DTS?
Thanks,
Rokal
You can do that in a DTS package using two data driven query tasks: one for the inserts and one for the updates. The data driven query tasks are a bit of a pain to use, but they work. I've also done this (a "merge") in sql server 2000 with an AS/400 database using a dynamic t-sql. You'd write a t-sql script that outputs psql and runs it againt a linked server to the Oracle database.
UPDATE:
A DTS "data driven query task" will let you insert|update data from the sql server connection in DTS to an oracle server connection in DTS w/o a temp table or a linked server.
Update2; here's some more info on what I mean:
http://www.databasejournal.com/features/mssql/article.php/3315951
http://msdn.microsoft.com/en-us/library/aa933507(SQL.80).aspx
Are you keeping the same primary key values?
If you are you have a number of options, some versions of SQL support the MERGE statement which will update or insert just like you require.
Or you can write your own.
Something along the lines of loading all the rows into a staging table in your SQL database and row by row checking for the existence of your primary key in your main SQL table. If the key exists update the row and if not insert it.
Yes, the primary key values in the source and destination will match.
I was hoping to accomplish this task without the use of a temporary (staging) table.
Also, I am using sql server 2000 so the MERGE statement is not available.
Try:
DELETE FROM dbo.WhateverTable WHERE WhateverTableID IN (SELECT WhateverTableID FROM MySource)
It might be pretty slow, use join instead:
Delete a
from firstTable a join secondTable b on a.id = b.id
There's no way with TSQL to do a INSERT or UPDATE in the same statement, but you could very easily do it in two statements (as you mentioned above).
Statement 1:
DELETE FROM dbo.WhateverTable
WHERE WhateverTableID IN (SELECT WhateverTableID FROM MySource)
Statement 2:
INSERT INTO dbo.WhateverTable
SELECT * FROM MySource
Also, is there any reason why you don't want to use a temp table?