0

I am trying to create a UDF to get full_name in snowflake with the below code:

CREATE OR REPLACE FUNCTION LASTNAMEFIRST (LAST_NAME char(100),FIRST_NAME char(100))
   
RETURNS CHAR(1000)
   AS
'declare
 FULL_NAME varchar;
 begin
 FULL_NAME := FIRST_NAME || '' '' || LAST_NAME;
 return FULL_NAME;
 End';

I am getting Compilation of SQL UDF failed: SQL compilation error: syntax error line 2 at position 1 unexpected 'FULL_NAME'. error

Your help would be appreciated.

1 Answer 1

1

The Snowflake Scripting block is not necessary:

CREATE OR REPLACE FUNCTION LASTNAMEFIRST (LAST_NAME char(100),FIRST_NAME char(100))
RETURNS CHAR(1000)
AS
' FIRST_NAME || '' '' || LAST_NAME';

Call:

SELECT LASTNAMEFIRST('a', 'b');
-- b a

If the BEGIN/END block is required then stored procedure is the way to go:

CREATE OR REPLACE PROCEDURE LASTNAMEFIRST (LAST_NAME char(100),FIRST_NAME char(100))
RETURNS TEXT
LANGUAGE SQL
AS
$$
declare
 FULL_NAME varchar;
begin
 FULL_NAME := FIRST_NAME || ' ' || LAST_NAME;
 return FULL_NAME;
End;
 $$;
 
CALL LASTNAMEFIRST('a', 'b');
-- b a
Sign up to request clarification or add additional context in comments.

5 Comments

Hi @LuKasz, I want to use a variable in that case. because similar code with stored procedure working fine.
@mahadevdhyani General Usage: "A SQL UDF evaluates an arbitrary SQL expression and returns the result(s) of the expression." - keyword expression. Snowfalke Script block is not expression. Please post your exact requirement, if you need Snowflakce Script block then Stored procedure is way to go
Thanks, @Lukasz Szozda, can we use a variable to store this inside of UDF?
@mahadevdhyani I do not believe it is possible to use BEGIN END block as expression in the context of SQL UDF.
Hi @Lukasz Szozda, without begin end block can we pass this to a variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.