PostgreSQL表变量 [英] PostgreSQL table variable

查看:818
本文介绍了PostgreSQL表变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在T-SQL中是否有类似表变量的内容?
在Sql Server中,它看起来像这样:

Is there anything like table variables in T-SQL?
In Sql Server it looks like this:

DECLARE @ProductTotals TABLE
(
  ProductID int,
  Revenue money
)

然后在过程中,我可以:

Then in procedure I can:

INSERT INTO @ProductTotals (ProductID, Revenue)
  SELECT ProductID, SUM(UnitPrice * Quantity)
  FROM [Order Details]
  GROUP BY ProductID

并像普通表一样使用此变量进行操作.

And manipulate with this variable like an ordinary table.

这里是描述: http://odetocode.com/Articles/365.aspx

推荐答案

@Clodoaldo评论:在PostgreSQL中使用临时表.例如:

As @Clodoaldo commented: use a temporary table in PostgreSQL. For your example:

CREATE TEMP TABLE product_totals (
   product_id int
 , revenue money
);

手册中有关 CREATE TABLE 的更多信息在这里可以找到此报价:

More information in the manual about CREATE TABLE where you can find this quote:

如果指定,该表将被创建为临时表.暂时的 在会话结束时自动删除表,或者 (可选)在当前事务结束时(请参阅提交时" 以下).具有相同名称的现有永久表不可见 临时表存在的情况下跳转到当前会话,除非它们 用模式限定的名称引用.在上创建的任何索引 临时表也将自动成为临时表.

If specified, the table is created as a temporary table. Temporary tables are automatically dropped at the end of a session, or optionally at the end of the current transaction (see ON COMMIT below). Existing permanent tables with the same name are not visible to the current session while the temporary table exists, unless they are referenced with schema-qualified names. Any indexes created on a temporary table are automatically temporary as well.

未记录的表是PostgreSQL 9.1的某些相关功能.他们通过不写 WAL 来节省磁盘写操作.这是由Robert Haas讨论的功能.

Unlogged tables are a somewhat related feature of PostgreSQL 9.1. They save disk writes by not writing to WAL. Here is a discussion of the features by Robert Haas.

此外,关于money数据类型:

这篇关于PostgreSQL表变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆