Inserting null and string 'Null in trigger condition - mysql

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')

Related

Mysql Query to Stored Procedure

i have got access to a view of a datatable (mysql 8) which has the following values:
Values DB
now I tried to write an mysql query to fill up the Null values with the last value, because our visualisation tool cannot use null values and the lines in the graph will disappear.
SELECT UTCTIME,
case when Flammtemperatur1 is NULL then
#vorheriger_wert_Flammtemperatur1
else
#vorheriger_wert_Flammtemperatur1 := Flammtemperatur1
end as Flammtemperatur_1,
case when Flammtemperatur2 is NULL then
#vorheriger_wert_Flammtemperatur2
else
#vorheriger_wert_Flammtemperatur2 := Flammtemperatur2
end as Flammtemperatur_2,
case when Rauchgasventilator is NULL then
#vorheriger_wert_Rauchgasventilator
else
#vorheriger_wert_Rauchgasventilator := Rauchgasventilator
end as Rauchgasventilator_,
case when Rezirkulation is NULL then
#vorheriger_wert_Rezirkulation
else
#vorheriger_wert_Rezirkulation := Rezirkulation
end as FRezirkulation_
FROM pivot_test
order by utctime asc
That works fine, but i need to save this in a view or stored procedure, so that our tool can have access to it. Views are not possible because of the session variables.
Can someone please help me creating a Stored procedure for my problem? I have never written a SP before.
Thanks!
Result
CREATE PROCEDURE XXXXXName #Flammtemperatur1 DECIMAL (3,2), #Flammtemperatur2 DECIMAL (3,2)
, #Rauchgasventilator DECIMAL (3,2) ,#Rezirkulation DECIMAL (3,2)
AS
SELECT UTCTIME,
case when Flammtemperatur1 is NULL then
#vorheriger_wert_Flammtemperatur1
else
#vorheriger_wert_Flammtemperatur1 := Flammtemperatur1
end as Flammtemperatur_1,
case when Flammtemperatur2 is NULL then
#vorheriger_wert_Flammtemperatur2
else
#vorheriger_wert_Flammtemperatur2 := Flammtemperatur2
end as Flammtemperatur_2,
case when Rauchgasventilator is NULL then
#vorheriger_wert_Rauchgasventilator
else
#vorheriger_wert_Rauchgasventilator := Rauchgasventilator
end as Rauchgasventilator_,
case when Rezirkulation is NULL then
#vorheriger_wert_Rezirkulation
else
#vorheriger_wert_Rezirkulation := Rezirkulation
end as FRezirkulation_
FROM pivot_test
order by utctime asc

Unable to insert write data conditionaly inside trigger (IF, CONCAT, IFNULL) in MySQL

CREATE TRIGGER TR_Update_Member
AFTER UPDATE ON `member`
FOR EACH ROW
BEGIN
DECLARE changeNote VARCHAR(5000) DEFAULT '';
SET changeNote = IF(OLD.Name != NEW.Name,
CONCAT( changeNote,
'Name(',
IFNULL(OLD.Name, '--'),
'->',
IFNULL(NEW.Name, '--'),
'), '
),
changeNote);
SELECT TRIM(TRAILING ', ' FROM changeNote) INTO changeNote;
INSERT INTO `member_change_log`(`Name`) VALUES(changeNote)
END
The above trigger does not insert any data when the name contains null. Could anyone please what is wrong with my code.
The above trigger does not insert any data when the name contains null. Could anyone please what is wrong with my code.
If NEW.Name is NULL then OLD.Name != NEW.Name is NULL too, and IF() executes alternative variant, i.e. you obtain
SET changeNote = changeNote
Simply swap variants:
SET changeNote = IF( OLD.Name = NEW.Name, changeNote, CONCAT( ... ) );
If both OLD.Name and NEW.Name may be NULL then use null-safe compare operator <=> instead of regular compare =.

USE case statement on mixed value in SQL Server

How can I use a case statement that does something like
CASE
WHEN val IS STRING
THEN something
WHEN val > 0
THEN something
without throwing a conversion error? The column I'm dealing with is varchar, but it will either contain 'NO VALUE' or a number.
How about this...does this help
DECLARE #VAL VARCHAR(8)
SELECT #VAL = COLUMNNAME1 FROM TABLENAME1 WHERE SOMEOTHERCOL2 = SOMEVALUE;
SELECT
CASE
WHEN #VAL = 'NO VALUE' THEN NULL
WHEN CAST(#VAL AS BIGINT) > 0 THEN #VAL
ELSE 'LT 1'
END
FROM TABLE
WHERE COLUMNNAME1 = SOMEVALUE
By CASE'ing first and CAST'ing second, you will filter out the values that are causing the exception. Keep in mind this will still fail if some of the strings are anything other than 'NO VALUE'
SELECT CASE(so.val)
WHEN 'NO VALUE'
THEN null
ELSE CAST(so.val AS int)
END from dbo.SO41270497 so
Try:
DECLARE #myTable TABLE (Val varchar(10))
INSERT INTO #myTable VALUES ('No Value'),('1'),('2'),('3'),('4'),('5')
SELECT * FROM #myTable
SELECT CASE WHEN ISNUMERIC(Val) = 1 THEN CONVERT(int,Val) ELSE NULL END
AS Val FROM #myTable

How to retrieve all data from table in MySQL if column value providing null value?

I would like to retrieve all data from table in MySQL if column value providing null value?
I have a table like country.
How to write a query
If country = ' ' I need to display all data in table.
If country='algeria', it will display the particular data.
I need both queries in a single query.
i am getting result .but without providing variable how to write query...
declare #country1 varchar(30)
set #country1 = 'asd'
SELECT country_id, country
FROM country
WHERE ((#country1 = '') or (#country1 != '' and country.country= #country1));
nulls are different than empty strings (''), and should be checked explicitly with the is operator.
Assuming the variable you're passing is :country:
SELECT country_id, country
FROM country
WHERE (:country IS NULL) or (:country = country.country)
If you have an input like :country:
select
country.country_id, country.country
from
country
where
country.country = case when TRIM(:country) = '' or :country IS NULL then country.country else :country end
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(30))
BEGIN
SELECT
country.country_id, country.country
FROM
country
WHERE
country.country = CASE WHEN TRIM(x) = '' OR x IS NULL THEN country.country ELSE x END;
END;
$$
DELIMITER ;
call sp1('COUNTRY');

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;