Reputation: 626
I am new to postgreSQL database , can you explain me why i got syntax error ? I can't find any answer in documentation docs
CREATE OR REPLACE FUNCTION validation(string_to_match varchar [], pattern
varchar , validation_type varchar) RETURNS boolean AS $$
DECLARE a_length ALIAS FOR $1;
DECLARE result_validation ALIAS FOR $2;
BEGIN
CASE validation_type
WHEN 'login' THEN array_length (string_to_match , 1) INTO $1 RAISE NOTICE
'Array length is %', $1;
WHEN 'register' THEN array_length(string_to_match,1) INTO $1 RAISE NOTICE
'Array length is %', $1;
WHEN 'contact' THEN array_length(string_to_match,1) INTO $1 RAISE NOTICE
'Array length is %', $1;
END CASE;
END;
$$ lANGUAGE plpgsql;
ERROR: syntax error at or near "array_length" LINE 7: WHEN 'login' THEN array_length (string_to_match , 1) INTO... ^ SQL state: 42601 Character: 258
Upvotes: 0
Views: 885
Reputation: 42753
Probably something like this you want(?) :
CREATE OR REPLACE FUNCTION validation(string_to_match varchar [], pattern
varchar , validation_type varchar)
RETURNS boolean
AS $$
DECLARE a_length INT;
BEGIN
IF validation_type = 'login' THEN
a_length := array_length(string_to_match , 1);
RAISE NOTICE 'Array length is %', a_length;
ELSIF validation_type = 'register' THEN
a_length := array_length(string_to_match , 1);
RAISE NOTICE 'Array length is %', a_length;
ELSIF validation_type = 'contact' THEN
a_length := array_length(string_to_match , 1);
RAISE NOTICE 'Array length is %', a_length;
END IF;
RETURN true; -- or false ?
END;
$$ lANGUAGE plpgsql;
Upvotes: 2
Reputation: 6289
CASE WHEN THEN END
is not a statement, but an expression.RAISE
cannot be used inside.INTO
variable means?Upvotes: 0
Reputation: 11
it's just a first intuiton but I think it's because of the space between array_length and (string_to_match, 1)
Upvotes: 0