Ich kann keine klare Erklärung der Syntax zu erstellen (und verwenden) Tabellen nur für die Innenberechnungen einer Funktion. Könnte mir bitte jemand ein Syntaxbeispiel geben?Temporäre Tabelle Postgresql-Funktion
Von dem, was ich gefunden habe, habe ich schon versucht, diese (mit und ohne @
vor temp_table
):
CREATE FUNCTION test.myfunction()
RETURNS SETOF test.out_table
AS $$
DECLARE @temp_table TABLE
(
id int,
value text
)
BEGIN
INSERT INTO @temp_table
SELECT id, value
FROM test.another_table;
INSERT INTO test.out_table
SELECT id, value
FROM @temp_table;
RETURN END
$$ LANGUAGE SQL;
ich:
ERROR: syntax error at or near "DECLARE" LINE 5: DECLARE @temp_table TABLE
-
Ich habe auch versucht die CREATE TABLE Ansatz vorgeschlagen here, auf diese Weise:
CREATE FUNCTION test.myfunction()
RETURNS SETOF test.out_table
AS $$
CREATE TABLE temp_table AS
SELECT id, value
FROM test.another_table;
INSERT INTO test.out_table
SELECT id, value
FROM temp_table;
$$ LANGUAGE SQL;
Und ich bekomme diese:
ERROR: relation "temp_table " does not exist LINE 11: FROM temp_table
(Natürlich, ich bin mir bewusst, die temp_table nicht notwendig ist für das, was ich in dem obigen Code machte, aber das ist nicht der Punkt :) => Ich mag die Syntax zu verstehen, um es)
Postgres verwendet temporäre Tabellen für diesen Zweck. Tabellenvariablen sind eine Funktion von SQL Server. –
Wo im Handbuch haben Sie die Syntax 'DECLARE @temp_table TABLE ...' gefunden? –