0

I wrote a simple query and it works. I want to make it a function. But it gives me an error -

SQL Error [42601]: An unexpected token "DISTINCT" was found following "BEGIN
 RETURN SELECT".  Expected tokens may include:  "(".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.26.14

The function is -

CREATE OR REPLACE FUNCTION CSE532.NEIGHBOR_LIST (IN_ZIP VARCHAR(5))
RETURNS TABLE (NEIGHBOR VARCHAR(5))
BEGIN
    RETURN SELECT DISTINCT substr(b.GEOID10,1,5) FROM CSE532.USZIP a, CSE532.USZIP b WHERE db2gse.st_intersects(a.SHAPE,b.SHAPE) = 1 AND substr(a.GEOID10,1,5) = IN_ZIP;
END

All the columns exist

5
  • Does it work if you remove the DISTINCT? Commented Apr 5, 2020 at 5:20
  • What tool do you use to run this statement? Change the statement separator in your tool to, let’s say, @, place it at the end of the statement and try again. Commented Apr 5, 2020 at 6:07
  • @mark-barinstein DBeaver is the gui I use. Let me try with your suggestion. Commented Apr 5, 2020 at 8:44
  • @CaiusJard doesnt work Commented Apr 5, 2020 at 14:40
  • When you remove the distinct what error do you see then? Commented Apr 5, 2020 at 18:56

1 Answer 1

1

Remove the BEGIN and END

CREATE OR REPLACE FUNCTION CSE532.NEIGHBOR_LIST (IN_ZIP VARCHAR(5))
RETURNS TABLE (NEIGHBOR VARCHAR(5))
    RETURN SELECT DISTINCT substr(b.GEOID10,1,5) FROM CSE532.USZIP a, CSE532.USZIP b WHERE db2gse.st_intersects(a.SHAPE,b.SHAPE) = 1 AND substr(a.GEOID10,1,5) = IN_ZIP

or use BEGIN ATOMIC

CREATE OR REPLACE FUNCTION CSE532.NEIGHBOR_LIST (IN_ZIP VARCHAR(5))
RETURNS TABLE (NEIGHBOR VARCHAR(5))
BEGIN ATOMIC
    RETURN SELECT DISTINCT substr(b.GEOID10,1,5) FROM CSE532.USZIP a, CSE532.USZIP b WHERE db2gse.st_intersects(a.SHAPE,b.SHAPE) = 1 AND substr(a.GEOID10,1,5) = IN_ZIP;
END
Sign up to request clarification or add additional context in comments.

Comments

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.