1

I'm trying to declare a text variable and insert it into my movie table.

This is what I'm doing:

DECLARE movie_plot TEXT;
movie_plot := '{test}';

INSERT INTO movie(plot) VALUES (movie_plot);

It gives me this error:

ERROR:  syntax error at or near "TEXT"
LINE 1: DECLARE movie_plot TEXT;
                           ^
SQL state: 42601
Character: 20

I already checked some solved questions similar to mine and this sintax seems correct. I saw that I could use the WITH to solve the problem, but i would like to use the DECLARE.

3
  • 1
    DECLARE (and thus variables) can only be used inside PL/pgSQL code not in SQL Commented Nov 29, 2021 at 22:17
  • "I already checked some solved questions similar to mine and this sintax seems correct." -- Not in SQL in Postgres, as @a_horse_with_no_name already commented. Maybe the other questions targeted another DBMS than Postgres. (SQL Server?). Maybe you want to explain the underlying problem, you try to solve with your approach, to get help on that. Commented Nov 29, 2021 at 22:20
  • But I am using Postgres, I'm writing my SQL on PgAdmin Commented Nov 30, 2021 at 9:36

1 Answer 1

2

Perhaps you want to use current_setting:

SET my_vars.movie_plot = 'test';
INSERT INTO movie(plot) VALUES (current_setting('my_vars.movie_plot'));
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.