E
E
eliasum2021-12-03 13:45:52
PostgreSQL
eliasum, 2021-12-03 13:45:52

How to escape table name?

Hello! How to properly escape the table name?
Now when asked:

CREATE OR REPLACE FUNCTION foo(_path VARCHAR)
RETURNS FLOAT AS $$
DECLARE 

  _Table TEXT;
  _Value FLOAT;
  
BEGIN

        IF (SELECT EXISTS(SELECT 1 from _glossary WHERE configuration = _path)) THEN
      _Table := (SELECT tablename FROM _glossary WHERE configuration = _path);
        ELSE
            _Table := NULL;
        END IF;  
  
    EXECUTE FORMAT('SELECT val FROM %I ORDER BY key DESC LIMIT 1', _Table) INTO _Value;
    RETURN _Value;
    
END;

$$ LANGUAGE plpgsql;


Here is the error: "ERROR: relation "NumberActual" does not exist"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimchik, 2021-12-04
@Dimpik

Usually the escape character is \

A
Anatoly, 2021-12-10
@TonyWrong

put the table name in quotes inside the quotes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question