I have the following simple select:
SELECT * FROM db.table WHERE uuid::varchar LIKE ('389519a6-ba79-4a4d-a696-c818808d3b26');
which works just fine in pgadmin v4.
Now I try to do the same with a plpgsql function:
queries[0] = "DROP FUNCTION createtemptable(text,text,text);"
+ "CREATE OR REPLACE FUNCTION createTempTable(name TEXT, id TEXT, units TEXT) "
+ "RETURNS void AS $$ "
+ "DECLARE "
+ "nameid TEXT; "
+ "BEGIN "
+ "nameid := name || id;"
+ "EXECUTE 'SET search_path = schema_name; "
+ "CREATE TABLE IF NOT EXISTS ' || quote_ident(nameid) || ' "
+ "AS SELECT * FROM ' || quote_ident(name) || ' "
+ "WHERE uuid LIKE ' || uuid(units) ;"
+ "END; $$ "
+ "LANGUAGE plpgsql;";
resulting in:
DROP FUNCTION createtemptable(text,text,text);
CREATE OR REPLACE FUNCTION createTempTable(name TEXT, id TEXT, units TEXT)
RETURNS void AS $$
DECLARE nameid TEXT;
BEGIN
nameid := name || id;
EXECUTE
'SET search_path = opsim;
CREATE TABLE IF NOT EXISTS ' || quote_ident(nameid) || ' AS
SELECT * FROM ' || quote_ident(name) || '
WHERE uuid LIKE ' || uuid(units);
END; $$ LANGUAGE plpgsql;
In the end I want to hand over an array of unit uuids and have a temp_table created for them. In the last few hours I traced the error back to the select on top.
What's weird: it works just fine in pgadmin, but fails with:
org.postgresql.util.PSQLException: Error: Syntaxerror at „a6“
It doesn't matter if I keep both as uuids, or compare them as strings, it always breaks at that point. And I can't find out why.
According to auntie google and everything I researched in the last 4 hours, it should work. Anyone an idea where I forgot to mask something or ... whatever?
EDIT: improved on suggestions from comments. I can't quote_ident units, as this would add unwanted quotes.
java? - talexquote_literal()andquote_ident()for quoting your inputs before embedding them in that query string or useformat()to quote and embed in one shot. - yieldsfalsehood