Update a Null column with conditional split null check - ssis

I am using conditional split to check if a row has changed or new. If new row insert it into Destination table or if existing row has been modified, update the same row.
New Rows ---ISNULL(Dest_EmpId)
Changed Rows (Id !=Dest_EmpId)||ISNULL(admin)
I have an existing column(admin) which is null and has been updated with a value (20200901). the conditional split is not able to evaluate it correctly, hence the unchanged row doesn't get updated.
Any help will be appreciated.

You have an OR condition on the IFF expression. With limited knowledge of the data that you are using and the design of the package in general, I would say that this is the only plausible explanation. Did you try changing the OR to AND and see if that helped?

Related

Is there a way of using the newly inserted rows id as a value in the rows column in MySQL in a simple way?

Is it possible to use the id of a newly inserted row inside the rows own column as a value in a simple way?
Or do I need to Insert it first, then then get lastInsertId, and then update the row?
There is no way to do that, you would need to do as you suggest and update the row again.
See: https://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
I would note, however, that this is probably a bad idea.

tracking actual changes in rows when updating table

I have a table which i keep on updating, from the values of other source in my code. The value I update may or may not be same as the value already in the row.
I need some kind of algorithm may be via mysql (db) or otherwise (part of code) so that I later may be able to identify which rows have a changed value.
There is a date modified column which I change. But that will not be a true indicator as it will always be updated. I want a way by which I can determine whether some predefined columns have changed values,
One solution is this: I can do a select query, then compare and update a changed flag in the table. But that seems complex and not for me as I have a table with a lot of records
Another solution might be to save the md5 checksum of the values in a column and while updating compare the previous md5 and current md5 and so on.
I want to know the best solution.
There's a fairly simple way to handle this problem. Let's think of it as managing when a row's timestamp gets updated.
First of all, as I'm sure you know, your table needs a timestamp column with default settings for INSERT and UPDATE. That looks like this if the column is called ts.
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Second, you can use an UPDATE query like this to change the values in a row.
UPDATE stock
SET val1 = 'newval1',
val2 = 'newval2',
changed_by = 'current_user_id'
WHERE id = 'id_to_change'
AND NOT (val1 == 'newval1' AND val2 == 'newval2')
The AND NOT clause on the WHERE will prevent the update from taking place unless 'newval1' or 'newval2' would actually provide new values. This works because no rows match the WHERE clause in the update.
When the update is prevented from taking place your automatically set ts column will not change. Neither will the changed_by column be set to the present user's user_id. So, you have the time and user of the most recent genuine change.
Also, many host language interfaces to MySQL have a method call to determine how many rows were affected by a recent UPDATE operation. With this technique, you'll get back zero rows when the row is not updated. That might be convenient for your user interface.
Also, this technique uses a single query, so it's safe if more than one user is trying to update the same row at the same time. There's no need to use a transaction to guarantee that.
(Note that tracking the changed_by user is optional, and will only work if your application can provide the current user's actual id.)
This is reasonably efficient as long as the database search for WHERE id = 'id_to_change' works quickly.
It does require reworking your application's UPDATE queries. But so would any other approach to this problem.

How can I see the final MySQL column value in a trigger even if it wasn't part of the update data?

How can I get the final value of a column in an AFTER UPDATE trigger even if I didn't update that specific column? Will NEW.columnname always have final value for that column?
If not, how can I get the final value?
On the MySQL Trigger Manual Page
it says the following, which, to me is inconclusive:
In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated.
Does NEW refer to ALL the data of the row or just the submitted data?
Does NEW refer to ALL the data of the row or just the submitted data?
Yes it (documentation) refers to all columns of a row that is being updated. Therefore you can safely use NEW keyword to address any columns, not only those that you actually updated.
Here is SQLFiddle demo. Although only col2 was updated all other columns were accessible.

Get the date when the row was inserted MySQL

Is there any way to get a datetime when the row was created? Is it stored anywhere? I just forgot to add a column which tells when the row was inserted, and lots of rows have been inserted and I don't know when they were inserted... Thanks.
Unfortunately there's no way to achieve that. MySQL doesn't store a timestamp of row creation, that's why you always have to create a column to do that, along with ON UPDATE clause which will update the column every time the row is updated
Nope. You need a column to record that, it's not stored in the system otherwise.
There isn't a way to get timestamp when the row was created unless you add a field that is populated with system time or has a default value.

Display a column only if it is not null

I need to display a particular column in my SQL result only if it is not null. If it is null, I don't want that column to appear at all in my result.
Is there a way to express this condition in SQL?
This wouldn't make sense because a query may return multiple rows. One row may have a value for the column in question and the next may not have a value. Then a conditional column would create a structural inconsistency between returned rows.
No, it is not...
It's not possible, and really unnecessary. You'll need to have a fixed number of columns, there's just no other way. But that isn't really your problem, you don't want that at all!
Queries are just to retrieve the data, not for the representation of the data. You should just retrieve it and hide the column if all the values are null.
SQL doesn't generally let you reason about properties of entire columns. Conditions are on properties of rows. So there's no way to say "if all the values in this set of this column are null...". However, you can trivially restrict yourself to rows that lack the property.
If you want to show a column only when it is not null for every row, you could do a COUNT(*) WHERE ... your general condition ... AND that_column IS NULL and then redo the query, including the column if the first result was 0 and excluding it otherwise. But I'm not sure why you'd want to do such a thing.