Delete records using Informatica PowerCenter - sql-server

I have simple mapping which deletes records in the target table. I have not used "UPDATE STRATEGY" transformation, rather set session property "delete" in order to delete records.
The said table is having composite primary keys (having 10 columns). It is working fine if all these columns are having value. BUT there are few records in which one of the column has NULL value. In this scenario, it is not deleting that record.
Can someone let me know how to handle this situation?

Its possible, because informatica fires sql like this when deleting data - delete from tab where key1=v1 and key2=v2. So, if v2 is null, its possible, delete query will ignore the record.
You can use target update override property to do this. Write your own sql to delete data.
DELETE FROM
mytable
WHERE
ID = :TU.ID
AND ISNULL(EMP_NAME,'Unspecified') = ISNULL(:TU.EMP_NAME,'Unspecified')
Since you have keys defined in infa you shouldnt face any problem. But please note that these deletes will be done on a row by row basis, so if it's a large table, and delete doesnt follow primary key index, the delete it could take time to delete each row!

Related

Use foreign keys to update/ delete matching records in multiple tables in Talend Open Studio for Data Integration

I'm struggling with deleting and updating multiple tables from the same input data.
Here is a screenshot of the job.
I'm comparing the rm table in the v24 MSSQL db and in the v26, if a record was deleted in the v24, it has to be deleted in the v26. This is what's happening with tMap_1 and it works. It finds the matching bl_id, fl_id and rm_id as keys.
But before deleting that record in the rm table, the three keys are used as foreign keys in other tables records and have to be deleted or updated.
In this case, I create a second row from tMap_1 which holds the bl_id, fl_id, rm_id that have to be adapted. I link it to a first table 'rmpct' and use a delete, it seems to work even though it's primary key is not used (I adapted the field options for the deletion keys).
But now what I'm trying to do is in the tables that have matching keys, I need to update the values of these keys in the record (they have to become empty). I tried it with another table here "activity_log" but it doesn't find the matching rows based on the foreign keys and I don't know how to update these values with different values than the input row.
Can somebody help me please?
Thanks!

Delete all rows in the table which is used as foreign key in other tables in ORACLE DB

I want to delete all the rows from a table in the Oracle DB i.e.Table Name:Address.
The table is used as the foreign key in other tables for example in Customers.
what i want is , When i delete all the rows of the table Address, All rows of other tables which are referencing these records should also be deleted.
NOTE I have not provided "on delete cascade" at the time of creating table.
Any help is appreciated.
That really depends on what you mean.
By your description you probably mean a cascading delete.
But that makes no sense, since your table is a foriegn key, so every "customer" would have an AddressID (int) column, and probably a NOT NULL column as well. so deleting all addresses would be ... deleting the entire customer table? or maybe DELETE FROM customer WHERE AddressID IS NOT NULL ? either way, that does not make sense.
Oh, I get it now. you are testing the boundaries of your ability. That actually makes sense in DEV environment. But make sure you don't do stuff like that in production. A couple of principles which I have found very good practice -
Don't delete. If you want to "delete" - simply add a
column IsDeleted bit NOT NULL DEFAULT (0)
because once a row is gone, it is gone forever. changing the row from IsDeleted=0 to IsDeleted=1 is always easily reversable.
Seperate in your mind with a clear line DML (data manipulation language - the act of changing data) with DDL (data definition language - the act of changing the table definitions etc. , aka - changing the schema). To delete all lines is to "reset" the table. Use Truncate TABLE Customers or DROP AND CREATE syntax. (but really - don't try this at home kids).

Spring Batch Update: Insert only if does not exist otherwise update

I need to write a batch update statement. I am able to do that. I dont have any primary key in my table. There are chances that duplicate data will be sent to database.
I want to write batch update in such a manner that It will insert only if data does not exist. When I say data does not exist, I mean 3 columns of the table which can uniquely identify a row. I don't want to make a primary key using these 3 columns.
Is there a way where we can write batch update which will insert only if data does not exist otherwise it will do the update.
I have tried merge query but could not get it.
Thanks
You can use an ItemProcessor to filter out duplicated items with a query, just return null if item is already present in database: objects that pass the processor can be written with ItemWriter and you are sure there are not duplicated

Sql server bulk delete by known record ids

I need to delete many rows from sql server 2008 database, it must be scalable so I was thinking about bulk delete, the problem is that the are not many references on this, at least in my case.
The first factor is that I will exactly know the ID of every row to delete, so any tips with TOP are not an option, also I will delete less rows that I want to retain so there will be no need for some "drop/temp table/re-create" methods.
So I was thinking to use WHERE IN , either suppling IDs or xml data with IDs, there is also an option to use MERGE to delete rows.
If I will have to delete 1000+ rows, sending all ids to WHERE IN could be a problem ? And what with MERGE - it is really a cure for all bulk insert/update/delete problems? What to choose?
One option would be to store the known ID's into a "controller" table, and then delete rows from your main data table that have their ID show up in the controller table.
That way, you could easily "batch" up your deletes, e.g.
DELETE FROM dbo.YourMainDataTable
WHERE ID IN (SELECT TOP (250) ID FROM dbo.DeleteControllerTable)
You could easily run this delete statement in e.g. a SQL Agent Job which comes around every 15 minutes to check if there's anything to delete. In the meantime, you could add more ID's to your DeleteController table, thus you could "decouple" the process of entering the ID's to be deleted from the actual deletion process.

Making primary key and identity column after data has been loaded

I have quick question for you SQL gurus. I have existing tables without primary key column and Identity is not set. Now I am trying to modify those tables by making existing integer column as primary key and adding identity values for that column. My question is should I first copy all the records from the table to a temp table before making those changes . Do I loose all the previous records if I ran the T-SQL commnad to make primary key and add identity column on those tables. What are the approaches should I take such as
1) Create temp table to copy all the records from the table to be modified
2) Load all the records to the temptable
3) Make changes on the table schema
4) Finally load the records from the temp table to the original table.
Or
there are better ways that this? I really appreciate your help
Thanks
Tools>Options>Designers>Table and Database Designers
Uncheck "Prevent saving changes that require table re-creation"
[Edit] I've tried this with populated tables and I didn't lose data, but I don't really know much about this.
Hopefully you don't have too many records in the table. What happens if you use Management studio to change an existing field to identity is that it creates another table with the identity field set. it turns identity insert on and inserets the records from the original table, then turns identity insert off. Then it drops the old table and renames the table it just created. This can be quite a lengthy process if you have many records. If so I would script this out and then do it in a job that runs during the off hours because the table will be completely locked while you do this.
just do all of your changes in management studio, copy/paste the generated script into a file. DON'T SAVE CHANGES at this point. Look over and edit that script as necessary, it will probably do almost exactly what you are thinking (it will drop the original table and rename the temp one to the original's name), but handle all constraints and FKs as well.
If your existing integer column is unique and suitable, there should be no problem converting it to a PK.
Another alternative, if you don't want to use the existing column, you can add a new PK columns to the main table, populate it and seed it, then run update statements to update all other tables with new PK.
Whatever way you do it, make sure you do a back-up first!!
You can always add the IDENTITY column after you have finished copying your data around. You can also then reset the IDENTITY seed to the max integer + 1. That should solve your problems.
DBCC CHECKIDENT ('MyTable', RESEED, n)
Where n is the number you want the identity to start at.

Resources