So say I have a user defined function:
CREATE FUNCTION ADDRESS_EXISTS_FULL (
line_1 VARCHAR(64),
line_2 VARCHAR(64),
city VARCHAR(64),
state VARCHAR(32),
zip VARCHAR(10),
type INT(3)
)
RETURNS INT(1)
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE aid INT;
SET aid = NULL;
SELECT addressid INTO aid FROM dlp.address as a
WHERE a.line_1 = line_1
AND a.line_2 = line_2
AND a.city = city
AND a.state = state
AND a.zip = zip
AND a.address_type = type
LIMIT 1;
RETURN IF(aid IS NOT NULL, 1, 0);
END$$
How do I make it so that some of these are not necessary? Like say if type is not needed, is there anyway to call
SELECT ADDRESS_EXISTS_FULL(l1,l2,c, s, z)
or would that fail? And how do I make that function work?
Thanks.
Note: I don’t need this function stored, it’s a one off so it’s in one big mysql script file.
You can rewrite the query inside stored function body to something like
Thus, if you pass
nullfor a particular parameter, it will be ignored. Also, I’d recommend avoiding naming parameters exactly the same as fields in the table – it may lead to very subtle bugs (In the query above I prepended ‘p_’ to each parameter).Finally, I’d not use
DETERMINISTICkeyword for a function that is non-deterministic by nature.