Comparison in mysql Trigger for Null Value - mysql

This is my trigger after update record.
The old.ActivatedDate always is Null and new.ActivatedDate always is not Null.
This is my code, but it is not working because its condition is not returning true to continue other jobs.
SET #OldActivateDate := OLD.ActivateDate;
SET #NewActivateDate := NEW.ActivateDate;
IF(#NewActivateDate IS NOT NULL AND #OldActivateDate IS NULL AND #OldActivateDate!=#NewActivateDate)
THEN
/* Do Some Functionality */
END IF;
I tested this method also, but again it is not working.
if (#NewActivateDate != #OldActivateDate) and ( #NewActivateDate is not null and #OldActivateDate is null )
THEN
/* Do Some Functionality */
END IF;

If you want to check that old.ActiveDate should be NULL and new.ActivateDate should not be NULL, then why do you have other conditions. Just use:
IF (#NewActivateDate IS NOT NULL AND #OldActivateDate IS NULL) THEN
. . .
END IF

Related

mysql trigger on update: how to detect NULL values?

I try to update the porcentageComplete profil when users updates his profile:
statut field:
`statut` enum('Marié','En couple','Divorcé') DEFAULT NULL,
Trigger is:
DROP TRIGGER IF EXISTS update_user;
DELIMITER $$
CREATE TRIGGER update_user
BEFORE UPDATE
ON users FOR EACH ROW
BEGIN
DECLARE porcentage int default 0;
IF NEW.statut!=NULL OR OLD.statut!=NULL THEN
set porcentage = porcentage + 1;
END IF;
SET NEW.porcentageCompleted = porcentage;
END$$
This not update correctly the porcentageCompleted : it looks like NULL comparaison does not work correctly
Comparisons with = or <>(or !=) to a NULL value yield NULL themselves, so not true, i.e. false. Use IS NULL and IS NOT NULL to check for NULLs.
...
IF NEW.statut IS NOT NULL
OR OLD.statut IS NOT NULL THEN
...
Worth a read: 3.3.4.6 Working with NULL Values

Why opposite operation <> doesn't work on trigger?

I have a trigger which is AFTER UPDATE. I have a condition in that which doesn't work, How can I fix it?
$$
BEGIN
IF NEW.colname <> OLD.colname THEN
DELETE FROM tableX WHERE id = OLD.id;
END IF;
END
$$
It should be noted if I write this IF 1 THEN then that DELETE query works. So the problem is that condition. What's wrong with it?
As #Darwin von Corax said in the comments, when one of these NEW.colname, OLD.colname is null, then all of that condition returns false. So I want to know how can I create a condition which acts like this ?
Null <> 10 -- true
Null <> Null -- false
10 <> 12 -- true
32 <> Null -- true
3 <> 3 -- false
If either NEW.colname or OLD.colname is null, then <> will return null which if would treat the same as false.
Fortunately, there is a solution: the <=> (NULL-safe equality) operator, which returns 1 if both operands are null and 0 if only one is. The expression
IF NOT (NEW.colname <=> OLD.colname) THEN
should do what you need.
Just add logic for checking null conditions. It is not triggered when both columns are null which means that no changes happen.
$$
BEGIN
IF
-- when exactly one of the column is null
NEW.colname is not null and OLD.colname is null or
NEW.colname is null and OLD.colname is not null or
-- when both are not null compare their values
NEW.colname is not null and OLD.colname is not null and NEW.colname <> OLD.colname THEN
DELETE FROM tableX WHERE id = OLD.id;
END IF;
END
$$

MySQL triggers integer variable

I want to do something like this:
DECLARE VAR_NAME INT DEFAULT 0;
IF NEW.field_name != NULL THEN
SET VAR_NAME := 8;
END IF;
IF NEW.field_name = NULL THEN
SET VAR_NAME := 1;
END IF;
UPDATE table_name SET column_name = column_name + VAR_NAME WHERE table_id = NEW.given_id;
I have a trigger for when inserting a new value:
I declare a variable, I change its value based on some conditions and then I update a table by adding this value to the current value of a specific column where the condition is met 'table_id = NEW.given_id'
Even I don't get errors the desired result is not happening.
Right, as the comments indicate, you can't check for NULL with normal equality operators. The way that I understand this is that nothing is equal to, less than, or greater than, etc, NULL, as NULL is literally 'no value'.
You must instead use IS NULL or IS NOT NULL to check for NULL.
This link goes into further detail:
http://dev.mysql.com/doc/refman/5.7/en/working-with-null.html

Inserting null and string 'Null in trigger condition

I'm creating a new trigger and want to have both null value and NULL string in :new.SCO_NUMBER validation. I'm getting error when i'm using both (as shown below) but when i use ':new.SCO_NUMBER IS NULL', it works fine. How to use or in this validation.
CREATE OR REPLACE TRIGGER TRIG_SCONUMBER_INSERT AFTER
INSERT ON S_SYN_EAI_SCO_IN FOR EACH row DECLARE XYZ BEGIN XYZ
SELECT XYZ
FROM xyz
WHERE xyz IF inserting
AND :new.SCO_NUMBER IS (NULL
OR 'NULL') THEN varError_Msg := 'SCO Number cannot be NULL in';
varError_id := 1;
varSucceeded := 'N' ;
varErrorExists :=1;
END IF;
In case of T-SQL,
replace new.SCO_NUMBER IS (NULL OR 'NULL') with new.SCO_NUMBER IS NULL OR new.SCO_NUMBER = 'NULL'. This should work for you.
Try this
IF inserting
AND ( :new.SCO_NUMBER IS NULL
OR :new.SCO_NUMBER = 'NULL') THEN
Try this,
(:new.SCO_NUMBER IS NULL OR :new.SCO_NUMBER = 'NULL')

IF ELSE with NULL values mysql

im having problems creating a function that must return the amount of goals of a team as a home and away
First it sums all local goals, and then it sums all visitor goals, and saves into variables, but the problem is, i want to know what can i do if there is NO data, i mean, what can i do when it returns NULL, i tried with IF ELSE but still not working, here is the code:
CREATE DEFINER=`root`#`localhost` FUNCTION `vgoles`(`veq` int) RETURNS int(11)
BEGIN
#Routine body goes here...
DECLARE vgloc INT;
DECLARE vgvis INT;
DECLARE vgoles INT;
SELECT SUM(gloc) INTO #vgloc FROM partidos WHERE eqloc=#veq;
SELECT SUM(gvis) INTO #vgvis FROM partidos WHERE eqvis=#veq;
IF #vgloc = NULL THEN
SET #vgloc = 0;
END IF;
IF #vgvis = NULL THEN
SET #vgvis = 0;
END IF;
SET #vgoles=#vgloc+#vgvis;
RETURN #vgoles;
END
Thanks and have a nice day
The IF #vgloc = NULL THEN does not work as you expect because you can't check NULL with equality (=). NULL is a special value that is not equal to anything, not even to itself.
SELECT (3 = NULL) --yields--> NULL
SELECT NOT (3 = NULL) --yields--> NULL
SELECT (NULL = NULL) --yields--> NULL
SELECT (NULL <> NULL) --yields--> NULL
SELECT (NULL IS NULL) --yields--> TRUE
To check for NULL value, you need: IF #vgloc IS NULL THEN.
But you can also use COALESCE() function for further simplicity:
SELECT COALESCE(SUM(gloc),0) INTO #vgloc FROM partidos WHERE eqloc=#veq;
SELECT COALESCE(SUM(gvis),0) INTO #vgvis FROM partidos WHERE eqvis=#veq;