0

I am trying to create tables which include the word "Messdaten" in it. Without ~ "Messdaten" part, code works well, but whenever I add ~ "Messdaten" it throws SQL State: 42601 sytax error. Do you have any idea what is wrong with the code? Its postgresql 9.2.

DECLARE
tbl_arr varchar[] := array['Messdaten2','dumptable'];
i varchar;
BEGIN
FOREACH i IN Array tbl_arr
LOOP
EXECUTE format('CREATE TABLE %I AS SELECT * FROM %I ~ "Messdaten"','backup_'||i,i);
END LOOP;
END;

1 Answer 1

1

The code is trying to do this:

CREATE TABLE Messdaten2 AS SELECT * FROM Messdaten2 ~ "Messdaten"

which is incorrect since Messadaten2 is the table name not the member of an expression.

Instead, put the regular expression check outside of the first parameter string of the format function:

IF i ~ 'Messdaten' THEN -- Do the regular expresssion test here
   EXECUTE format('CREATE TABLE %I AS SELECT * FROM %I','backup_'||i,i);
END IF;
Sign up to request clarification or add additional context in comments.

1 Comment

It works but what do you mean by outside of the code? outside of the sql query? I have tried to put the reqular expression to the foreach part which is also outside of the query but didn't work, I am confused now = ) BEGIN FOREACH i IN Array tbl_arr ~ 'Messdaten' LOOP

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.