0

I have a function with a dynamic copy statement and when I try to SELECT function, I get the error "query has no destination for result data"

select loadTodaysData();

CREATE FUNCTION loadTodaysData()
RETURNS VARCHAR(1000) AS $$
DECLARE vtoday VARCHAR(20) DEFAULT CURRENT_DATE;
BEGIN
  EXECUTE 'COPY nsn 
        FROM ''C:\Users\yuju\SQL_Data\test_''|| vtoday || ''.csv'' 
        with delimiter ','
        csv header';
END; $$
language plpgsql;

I want this to update my table everyday automatically from a folder. Function was created without an error.

2
  • Sorry code isn't formatted well in this post Commented Jun 6, 2024 at 16:01
  • A plain syntax problem. You didn't double all the single quotes in the statement string. Commented Jun 7, 2024 at 6:14

1 Answer 1

0

Your stored function does not return a value, so you have nothing to select. Either return a value, like returning vtoday from your stored function and then you will have something to select, or, if you just want to execute your command, then refactor your function into a stored procedure and then use the CALL statement like

CALL loadTodaysData();
Sign up to request clarification or add additional context in comments.

3 Comments

RETURN vtoday after the EXECUTE statement?
@YujuTakahashi yes...
Thanks for the reply, I am still getting the same error message

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.