I am new to SQL and wasn't able to find a direct way of using User Defined Variable without using stored procedure. SUBSTRING_INDEX(REFERENCE) is being called multiple times and it looks inefficient. Below is sample code from my previous question.
UPDATE TABLE
SET TYPE = (CASE WHEN SUBSTRING_INDEX(REFERENCE, '_', -1) = 'A'
THEN 1
WHEN SUBSTRING_INDEX(REFERENCE, '_', -1) = 'B'
THEN 2
ELSE TYPE
END),
COLOR = (CASE WHEN SUBSTRING_INDEX(REFERENCE, '_', -1) = 'A'
THEN 'BLUE'
WHEN SUBSTRING_INDEX(REFERENCE, '_', -1) = 'B'
THEN 'RED'
ELSE COLOR
...
Adding SET @rule_id = SUBSTRING_INDEX(CELERITY_MATCH_REF, '_', 1) FROM ilms_tran_cashflows_match
at the top and using @rule_id resulted in syntax error from HeidiSQL.
What is the correct way or this is not possible?