Using SELECT INTO [tableName] in stored procedure not working
I'm trying to use a table name in a
SELECT INT开发者_开发问答O [tableName]
But it complains that [tableName] is expected to be a record variable, row variable, or list of scalar variables. How can I put the result of the select into a table instead of a variable in a stored procedure?
As mentioned in the manual here:
http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW
you cannot use that syntax inside a pl/pgSQL procedure.
You need to use the following syntax:
CREATE TABLE new_table AS SELECT ....
You want an INSERT INTO ... SELECT query, not SELECT INTO. For instance :
INSERT INTO my_table (name, my_avg) SELECT name, AVG(col) FROM your_table GROUP BY name
Unless you actually wanted to create a new table, where SELECT INTO should work correctly :
vsavard_db=> SELECT 1 INTO b;
SELECT
Time : 47,268 ms
vsavard_db=> SELECT * FROM b;
?column?
----------
1
(1 line)
Your syntax is wrong, according to http://www.postgresql.org/docs/8.1/static/sql-selectinto.html your stored proc should follow this patter:
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
* | expression [ AS output_name ] [, ...]
INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY expression [, ...] ]
[ HAVING condition [, ...] ]
[ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
[ LIMIT { count | ALL } ]
[ OFFSET start ]
[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] ]
EX: SELECT * INTO my_table WHERE x = @myParam
精彩评论