问题如上
在sqlserver的存储过程中 有2中方式键临时表对象 1.declare @table1 table(id int,age int);2.SELECT id,name INTO #Table1 FROM (select * from myTable);在postgresql 中有没有类似方法???? 急急
在sqlserver的存储过程中 有2中方式键临时表对象 1.declare @table1 table(id int,age int);2.SELECT id,name INTO #Table1 FROM (select * from myTable);在postgresql 中有没有类似方法???? 急急
iihero=# select * from t1;
id | col2 | col3
----+------+------
1 | abc | 2
(1 row)
iihero=# create temporary table t_2 as select * from t1;
SELECT
iihero=# select * from t_2;
id | col2 | col3
----+------+------
1 | abc | 2
(1 row)
CREATE OR REPLACE FUNCTION temptable()
RETURNS SETOF record AS
$BODY$declarebegin
create temporary table "t_2" as select * from "MyTable";return query select * from "t_2";end;$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION temptable() OWNER TO postgres;在pgAdmin的查询工具中执行该函数select * from "temptable"() as tt(a integer,b integer);提示错误:
ERROR: relation "ta1" already exists
CONTEXT: SQL statement "create temporary table "ta1" (a integer, b integer)"
PL/pgSQL function "temptable" line 7 at SQL statement
那里出错了????
CONTEXT: SQL statement "create temporary table "t_2" as select * from "Counter_Table""
PL/pgSQL function "temptable" line 13 at SQL statement
2楼 4楼的说法都是对的 包括我发的代码也是正确的 只是在查询工具里 执行第一次select * from "temptable"() as tt(a integer,b integer);
返回结果正确 之后在运行就会发生我上面发的哪个错误。错误猜测:临时表只在当前会话内可见 在同一会话内创建同名表 会出错。。!至于什么是会话俺还不清楚。。(个人觉得 连接开始 - 结束 是一次会话)