“选择临时表”是什么意思?意思是?
这个答案让我有点困惑。什么是“选择临时表”,有人可以向我展示一个简单的示例吗?
This answer had me slightly confused. What is a 'select to a temp table' and can someone show me a simple example of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
临时表是仅在存储过程期间存在的表,通常用于保存最终计算过程中的临时结果。
在 SQL Server 中,所有临时表都以 # 为前缀,因此如果您发出类似的语句,
那么 SQL Server 将自动知道该表是临时的,并且当存储过程超出范围时它将被销毁,除非显式删除该表就像
我通常在针对具有高事务量的大型表运行的存储过程中使用它们一样,因为我可以将所需的数据子集作为临时副本插入到临时表中,并处理数据,而不必担心降低生产效率如果我对数据所做的操作是相当密集的操作。
在 SQL Server 中,所有临时表都位于 tempdb 数据集中。
有关详细信息,请参阅本文。
A temp table is a table that exists just for the duration of the stored procedure and is commonly used to hold temporary results on the way to a final calculation.
In SQL Server, all temp tables are prefixed with a # so if you issue a statement like
Then SQL Server will automatically know that the table is temporary, and it will be destroyed when the stored procedure goes out of scope unless the table is explicitly dropped like
I commonly use them in stored procedures that run against huge tables with a high transaction volume, because I can insert the subset of data that I need into the temp table as a temporary copy and work on the data without fear of bringing down a production system if what I'm doing with the data is a fairly intense operation.
In SQL Server all temp tables live in the tempdb datase.
See this article for more information.
如果您有一组复杂的结果想要一次又一次地使用,那么您是否继续查询主表(其中数据会发生变化,并且可能会影响性能),或者是否将它们存储在临时表中以进行更多处理。最好经常使用临时表。
或者您确实需要以非设置方式迭代行,您可以使用临时表(或游标)
如果您对数据库进行简单的CRUD,那么您可能不需要临时表
您有:
DECLARE @foo TABLE (bar int...)
CREATE TABLE #foo (bar int...)
选择...进入#foo FROM...
If you have a complex set of results that you want to use again and again, then do you keep querying the main tables (where data will be changing, and may impact performance) or do you store them up in a temporary table for more processing. It's better to use a temporary table often.
Or you really need to iterate through rows in a non-set fashion you can use a temp table (or CURSOR)
If you do simple CRUD against a DB then you probably have no need for temp tables
You have:
DECLARE @foo TABLE (bar int...)
CREATE TABLE #foo (bar int...)
SELECT ... INTO #foo FROM...
临时表是使用某些此类语法动态创建的表:
您将拥有一个用您选择的值填充的表。现在您可以选择它、更新它等等。
该表存在于某个预定的时间范围内,例如,存在于其所在的存储过程的持续时间内。然后它就会从记忆中消失,再也无法访问。暂时的。
华泰
A temp table is a table that is dynamically created by using some such syntax:
What you then have is a table that is populated with the values that you selected into it. Now you can select against it, update it, whatever.
The table lives for some predetermined scope of time, for example, for the duration of the stored procedure in which it lives. Then it's gone from memory and never accessible again. Temporary.
HTH
您可以使用
SELECT ... INTO
创建临时表并填充它,如下所示:(顺便说一句,此语法适用于SQL Server和Sybase。)
编辑一旦您像我上面那样创建了表,然后您可以在同一连接上使用它进行其他查询:
这里的关键是这一切都发生在同一连接上。因此,从客户端脚本创建和使用临时表会很尴尬,因为您必须确保该表的所有后续使用都在同一连接上。相反,大多数人在存储过程中使用临时表,他们在一行上创建表,然后在同一过程中稍后使用几行。
You can use
SELECT ... INTO
to both create a temp table and populate it like so:(BTW, this syntax is for SQL Server and Sybase. )
EDIT Once you had created the table like I did above, you can then use it other queries on the same connection:
The key here is that it all happens on the same connection. Thus, to create and use a temp table from a client script would be awkward in that you would have to ensure that all subsequent uses of the table were on the same connection. Instead, most people use temp tables in stored procedures where they create the table on one line and then use a few lines later in the same procedure.
将临时表视为“table”类型的 sql 变量。在脚本和存储过程中使用它们。当您需要操作的数据不是简单值而是数据库表(垂直和水平)的子集时,它会很方便。
当您意识到这些好处时,您就可以利用临时表的各种共享模型(范围)带来的更多功能:私有、全局、事务等。所有主要 RDBMS 引擎都支持临时表,但没有标准功能或语法他们。
有关用法示例,请参阅答案。
Think of temp tables as sql variable of type 'table'. Use them in scripts and stored procedures. It comes handy when you need to manipulate data that is not simple value but a subset of a database table (both vertical and horizontal).
When you realize these benefits then you can take advantage of more power that comes with various sharing models (scope) for temp tables: private, global, transaction, etc. All major RDBMS engines support temp tables but there is no standard features or syntax for them.
For example of usage see answer.