How to write SQL function in SQL script

deep_215
Level 2
How to write SQL function in SQL script

Hi,

I am trying to write a SQL function in SQL script

CREATE OR REPLACE FUNCTION fun1() RETURNS VOID AS

DECLARE
VAR_VALUE INTEGER;
e RECORD;
BEGIN
for e in select col1,col2 from table
Loop
--BODY OF FUNCTION
END LOOP;
END; 

I am getting error while running the code in DECLARE section stating unexpected <EOF>.

Can anyone help me to get rid of this issue and help me in executing the SQL Script for SQL function.

 

Thanks in advance.

Regards,

Deepak

0 Kudos
3 Replies
Turribeach

What SQL engine do you use?

0 Kudos
deep_215
Level 2
Author

I am using Snowflake

0 Kudos
Turribeach

And it's that how you create a SQL function in Snowflake? (ie the correct syntax):

https://docs.snowflake.com/en/developer-guide/udf/sql/udf-sql-introduction

It looks like you copied the syntax from this documentation page which is showing how to create a function in PL/pgSQL which is PostgreSQL Procedural languange.  

Each SQL engine has it's own SQL syntax, it's own SQL procedural language, etc. You can't mix and match. 

0 Kudos