I am porting some MSSQL queries over to PostgreSQL...
# sql
m
I am porting some MSSQL queries over to PostgreSQL (running on ACF) and having problems. I am running this query using queryexecute:
Copy code
result=queryexecute(sql,[],{datasource:dsnname});
Copy code
DO $$DECLARE tid integer; BEGIN UPDATE some_table SET somefield='text', updated_date=CURRENT_TIMESTAMP where pk=2386 RETURNING pk INTO tid; END$$;
The query technically executes ok, as the value is updated in the db, but the return value is undefined. my original MSSQL code had a select statement at the end to return some fields. what am I doing wrong?
m
What are you expecting this to return?
m
the primary key value... with the old MS SQL code, I had a select pk as the final statement, so my return would contain that. but postgres doesn't like the select statement at the end.
m
can you elaborate on "doesn't like"?
Is
2386
not the PK? Can you run a separate query to return this?
m
The pk isn't always passed, since sometimes the main record is INSERTed, and thus the pk is used for subsequent records... the query is simplified a great deal, but yes, 2386 is the PK. The nature of my problem is trying to figure out how to get return data back to CF. So, if when I include SELECT tid; as a final statement, postgres returns error: query has no destination for result data Hint: if you want to discard the results of a SELECT, use PERFORM isntead.
it seems the DO block always returns a void, so that explains that. I may have to create a temporary function and run that.