问题如上
 在sqlserver的存储过程中 有2中方式键临时表对象 1.declare @table1 table(id int,age int);2.SELECT id,name INTO #Table1 FROM (select * from myTable);在postgresql 中有没有类似方法???? 急急

解决方案 »

  1.   

    参考下例:(t_2即为临时表)
    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)
      

  2.   

    没有,只有通过函数返回reocrd OR refcursor
      

  3.   

    iihero=# create temporary table t_2 as select * from t1;#2楼 正解。建议楼主可以下载一个 "PostgreSQL 8.4.0 Documentation" 参考一下。
      

  4.   

    按照2楼的说法 我试了一下 出错不知道是那里弄错了???
    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  
    那里出错了????
      

  5.   

    上面哪个错误发错了ERROR:  relation "t_2" already exists
    CONTEXT:  SQL statement "create temporary table "t_2" as select * from "Counter_Table""
    PL/pgSQL function "temptable" line 13 at SQL statement
      

  6.   

    ERROR:  relation "t_2" already exists这个表已经存在了。先 drop table t_2;
      

  7.   

    结贴:
     2楼 4楼的说法都是对的 包括我发的代码也是正确的 只是在查询工具里 执行第一次select * from "temptable"() as tt(a integer,b integer);
    返回结果正确 之后在运行就会发生我上面发的哪个错误。错误猜测:临时表只在当前会话内可见 在同一会话内创建同名表 会出错。。!至于什么是会话俺还不清楚。。(个人觉得 连接开始 - 结束 是一次会话)