[英]When to use temporary table in SQL Server 2005
I read about temporary tables, global temporary tables and table variables.我阅读了临时表、全局临时表和表变量。 I understood it but could not imagine a condition when I have to use this.
我理解它,但无法想象我必须使用它的条件。 Please elaborate on when I should use the temporary table.
请详细说明我什么时候应该使用临时表。
Most common scenario for using temporary tables is from within a stored procedure.使用临时表的最常见场景是在存储过程中。
If there is logic inside a stored procedure which involves manipulation of data that cannot be done within a single query, then in such cases, the output of one query / intermediate results can be stored in a temporary table which then participates in further manipulation via joins etc to achieve the final result.如果存储过程中的逻辑涉及无法在单个查询中完成的数据操作,那么在这种情况下,一个查询/中间结果的输出可以存储在临时表中,然后通过连接参与进一步的操作等以达到最终结果。
One common scenario in using temporary tables is to store the results of a SELECT INTO statement使用临时表的一种常见场景是存储 SELECT INTO 语句的结果
The table variable is relatively new (introduced in SQL Server 2005 - as far as i can remember ) can be used instead of the temp table in most cases.表变量相对较新(在 SQL Server 2005 中引入 - 据我所知)在大多数情况下可以用来代替临时表。 Some differences between the two are discussed here
此处讨论了两者之间的一些差异
In a lot of cases, especially in OLTP applications, usage of temporary tables within your procedures means that you MAY possibly have business processing logic in your database and might be a consideration for you to re-look your design - especially in case of n tier systems having a separate business layer in their application.在很多情况下,尤其是在OLTP应用程序,临时表的使用中你的程序的手段,你可能有业务处理逻辑数据库,并为你重新看你的设计可能是一个考虑因素-尤其是在正级的情况下,在其应用程序中具有单独业务层的系统。
The main difference between the three is a matter of lifetime and scope.三者之间的主要区别是生命周期和范围的问题。
By a global table, I am assuming you mean a standard, run of the mill, table.通过全局表,我假设您的意思是标准的、工厂运行的表。 Tables are used for storing persistent data.
表用于存储持久数据。 They are accessible to all logged in users.
所有登录用户都可以访问它们。 Any changes you make are visible to other users and vice versa.
您所做的任何更改对其他用户都是可见的,反之亦然。
A temporary table exist solely for storing data within a session.临时表仅用于在会话中存储数据。 The best time to use temporary tables are when you need to store information within SQL server for use over a number of SQL transactions.
使用临时表的最佳时间是您需要在 SQL Server 中存储信息以供多个 SQL 事务使用时。 Like a normal table, you'll create it, interact with it (insert/update/delete) and when you are done, you'll drop it.
像普通表一样,您将创建它,与之交互(插入/更新/删除),完成后,您将删除它。 There are two differences between a table and a temporary table.
表和临时表之间有两个区别。
A table variable works like any variable within SQL Server.表变量的工作方式类似于 SQL Server 中的任何变量。 This is used for storing data for use in a single transaction.
这用于存储在单个事务中使用的数据。 This is a relatively new feature of TSQL and is generally used for passing data between procedures - like passing an array.
这是 TSQL 的一个相对较新的特性,通常用于在过程之间传递数据——比如传递数组。 There are three differences between a table and a table variable.
表和表变量之间存在三个区别。
I personally avoid using temporary tables and table variables, for a few reasons.我个人避免使用临时表和表变量,原因有几个。 First, the syntax for them is Microsoft specific.
首先,它们的语法是 Microsoft 特定的。 If your program is going to interact with more than one RDBMS, don't use them.
如果您的程序要与多个 RDBMS 交互,请不要使用它们。 Also, temporary tables and table variables have a tendency to increase the complexity of some SQL queries.
此外,临时表和表变量有增加某些 SQL 查询复杂性的趋势。 If your code can be accomplished using a simpler method, I'd recommend going with simple.
如果您的代码可以使用更简单的方法完成,我建议您使用 simple。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.