I'm having troubles getting the following simple script to run in VS Code (using the MySQL plugin) on Postgres 16rc1.
DO $$ BEGIN
-- Declare a variable of user-defined type utTable.
DECLARE data1 mySchema.utTable;
-- Prefill the variable for use in a function call
INSERT INTO data1(utCol1, utCol2, utCol3)
VALUES
('a0', 1, 'test value 0'),
('a1', 1, 'test value 1'),
('a2', 1, 'test value 2a'),
('a2', 2, 'test value 2b'),
('a3', 1, 'test value 3');
-- Call a function and pass the prefilled variable to it
SELECT * FROM CALL mySchema.fnDoStuff('blah', data1);
END $$;
When I execute the script, I get the following error message syntax error at or near "INTO"
I have tried with just a BEGIN block, I also tried changing the INSERT INTO for a SELECT ... INTO, but nothing I've tried worked.
Update:
Yes, I'm in the process of trying to convert my project from SQL Server to Postgres, and unlearning stuff is not always easy.
Regarding the comments stating that there are no table variables, that might be the case, however both Functions and Procedures can accept table parameters - so, what is the "best practice" approach to filling a variable (table) that I can pass to a function or procedure within a small script?
Further more, regarding the comments that DO can't return anything - I was only trying to use the DO because within the VS Code editor, when I don't use the DO all my statements get separated into individual statements, which then causes a problem for the INSERT as the varaible data1 is then unknown.
The sole purpose of the script is to test the function that I created. The function is intended to be called from a Java application, however before I start on the Java code, I wanted to be able to test and confirm that the function works as intended, and for that I need to be able to pass a small set of test data to it - the table.
SELECTat the end is only there for my own debugging - a variation of the code was originally writte for T-SQL. Regarding your single query, how would that go?DOcommand. Syntax and the whole approach are not right. For starters, there are no table variables in PL/pgSQL. See: stackoverflow.com/a/10790059/939860, stackoverflow.com/a/34034929/939860 Next, you cannot return from aDOcommand. stackoverflow.com/a/14653151/939860 You have to rethink your approach.