Merge not working when using condtion of rowid in oracle - database

I have a person table as:
I have another table temp_person
I want to merge the data of temp_person to person table when their rowid matches.So,I tried:
MERGE INTO OT.person A
USING OT.TEMP_person B
ON (A.rowid=B.rowid)
WHEN MATCHED THEN
UPDATE SET A.age=B.age
But it says:
0 rows merged.
Why are the rows not being merged?

Related

Best approach for MERGE in Azure Synapse

I have two tables(src, tgt) on a dedicated sql pool in Azure Synapse, which both contain approximately 200 million records. Let's say they have columns A and B. I'm trying to update the field tgt.A with the value from src.A when tgt.B matches src.B. If src.B does not exist in tgt.B, insert the new record.
I tried using the MERGE statement as below:
MERGE tgt
USING src
ON tgt.B = src.B
WHEN MATCHED AND tgt.A <> src.A
THEN UPDATE SET tgt.A = src.A
WHEN NOT MATCHED
THEN INSERT (
A,B
)
Values
(
src.A, src.B
);
I also tried the insert and update statements separately as below:
INSERT INTO tgt
SELECT A, B
FROM src
WHERE NOT EXISTS(SELECT B FROM tgt WHERE tgt.B = src.B)
UPDATE tgt
SET tgt.A = src.A
WHERE EXISTS(SELECT B FROM tgt WHERE tgt.B = src.B)
For testing purpose, I took a smaller subset of tgt table containing 6 million records and source containing only 400k records. Still, with either of the above approaches, the stored procedure keeps executing beyond 1 hour where I cancel the execution. I noticed that with even smaller samples with 1k records in each table, it executes within a minute but struggles with large number of records. Please recommend the best approach to tackle the issue, can this be optimized?
Note: The source table is populated and dropped within the stored procedure and only the target table is saved with the updated records.

Create a cross reference table using Golden record and associate other records to the record

I have a table where I have ranked all the rows based on the created date column and have the rank indicated on the table as below
main table
I would like to create a cross-reference table with the golden record as the recurring column and the other two records as associated records as below.
output desired
I would like to know how I can achieve this using SQL.
I have tried creating a seperate table with all ID numbers (Rank = 1) and then joining it with the main table to get the ones with rank 1,2 and 3 associated with it. But it doesnt seem to work as I intend to.
output
I have not tested but something like this should work. You might want to add a name_id field.
select b.id_number,a.id_number
from table a
join table b on a.name=b.name
where b.rank=1

compare 2 tables if match update if not match insert

I have 2 tables with same table structure. Table A is having all transaction with 3 unique key in each record. Table B have only condition base record only.
I want compare both tables if Table B has matching record than I want to update and Table B have not matching record than insert in Table B.
Can you please suggest best way to do it like ssis or any thing else
The easiest way is a MERGE statement:
MERGE INTO Table_B
USING Table_A
ON TableA.ID1 = Table_B.ID1 AND TableA.ID2 = Table_B.ID2 AND TableA.ID3 = Table_B.ID3
WHEN MATCHED THEN UPDATE SET A = Table_A.A, B = Table_A.B -- Etcetera...
WHEN NOT MATCHED THEN INSERT (A, B) VALUES (Table_A.a, Table_A.B) -- Etcetera...
WHEN NOT MATCHED BY SOURCE THEN DELETE -- If Necessary...
;
By the way, don't forget the ";" at the end. SQL Server doesn't usually need them, but a MERGE does.

SSIS : delete rows after an update or insert

Here is the following situation:
I have a table of StudentsA which needs to be synchronized with another table, on a different server, StudentsB. It's a one-way sync from A to B.
Since the table StudentsA can hold a large number of rows, we have a table called StudentsSync (on the input server) containing the ID of StudentsA which have been modified since the last copy from StudentsA to StudentsB.
I made the following SSIS Data Flow task:
The only problem is that I need to DELETE the row from StudentsSync after a successful copy or update. Something like this:
Any idea how this can be achieved?
It can be achieved using 3 methods
1.If your target table in OutputDB has TimeStamp columns such as Create and modified TimeStamp then rows which have got updated or inserted can be obtained by writing a simple query. You need to write the below query in the execte sql task in Control Flow to delete those rows in Sync Table .
Delete from SyncTable
where keyColumn in (Select primary_key from target
where ModifiedTimeStamp >= GETDATE() or (ModifiedTimeStamp is null
and CreateTimeStamp>=GETDATE()))
I assume StudentsA's primary key is present in Sync table along with primary key of Target table. The above condition basically checks, if a new row is added then CreateTimeStamp column will have current date and modifiedTimeStamp will be null else if the values are updated then the modifiedTimeStamp will have current date
The above query will work if you have TimeStamp columns in your target table which i feel should be there if your loading data into Data Warehouse
2.You can use MERGE syntax to perform the update and insert in Control Flow with Execute SQL Task.No need to use Data Flow Task .The below query can be used even if you don't have any TimeStamp columns
DECLARE #Output TABLE ( ActionType VARCHAR(20), SourcePrimaryKey INT)
MERGE StudentsB AS TARGET
USING StudentsA AS SOURCE
ON (TARGET.CommonColumn = SOURCE.CommonColumn)
WHEN MATCHED
THEN
UPDATE SET TARGET.column = SOURCE.Column,TARGET.ModifiedTimeStamp=GETDATE()
WHEN NOT MATCHED BY TARGET THEN
INSERT (col1,col2,Col3)
VALUES (SOURCE.col1, SOURCE.col2, SOURCE.Col3)
OUTPUT $action,
INSERTED.PrimaryKey AS SourcePrimaryKey INTO #Output
Delete from SyncTable
where PrimaryKey in (Select SourcePrimaryKey from #Output
where ActionType ='INSERT' or ActionType='UPDATE')
The code is not tested as i'm running out of time .but at-least it should give you a fair idea how to proceed . .For furthur detail on MERGE syntax read this and this
3.Use Multicast Component to duplicate the dataset for Insert and Update .Connect a MULTICAST to lookmatch output and another multicast to Lookup No match output
Add a task after "Update existing entry" and after "Insert new entry" to add the student ID to a variable which will contain the list of IDs to delete.
Enclose all of the tasks in a sequence container.
After the sequence container executes add a task to delete all the records from the sync table that are in that variable you've been populating.

delete duplicate rows

anyone know how can i delete duplicate rows by writing new way from script below to improve performance.
DELETE lt1 FROM #listingsTemp lt1, #listingsTemp lt2
WHERE lt1.code = lt2.code and lt1.classification_id > lt2.classification_id and (lt1.fap < lt2.fap or lt1.fap = lt2.fap)
Delete Duplicate Rows in a SQL Table :
delete table_a
where rowid not in
(select min(rowid) from table_a
group by column1, column2);
1 - Create an Identity Column (ID) for your table (t1)
2 - Do a Group by on your table with your conditions and get IDs of duplicated records.
3 - Now, simply Delete records from t1 where IDs IN duplicated IDs set.
Look into BINARY_CHECKSUM .... you could possibly use it when creating your temp tables to more quickly determine if the data is the same.... for example create a new field in both temp tables storing the binary_checksum value... then just delete where those fields equal
The odiseh answer seems to be valid (+1), but if for some reason you can't alter the structure of the table (because you have not the code of the applications that are using it or something) you could write a job that run every night and delete the duplicates (using the Moayad Mardini code).

Resources