DROP FUNCTION IF EXISTS mocha_get_user_by_username;

CREATE FUNCTION mocha_get_user_by_username
(
    p_user_name VARCHAR(32)
)
RETURNS INT
RETURN (
	SELECT src_inst_id FROM mocha_attributes
	WHERE (tenant_id = mocha_get_current_tenant() OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = mocha_get_current_tenant()))
	AND att_inst_id = mocha_get_instance_by_global_identifier('960FAF025C5940F791A720012A99D9ED')
	AND att_value = p_user_name
	ORDER BY att_effective_date DESC
	LIMIT 1);