简体   繁体   English

在 Sql Server 2008 中截断/清除表变量

[英]Truncate/Clear table variable in Sql Server 2008

Is it possible to truncate or flush out a table variable in sql server 2008 ?是否可以在 sql server 2008 中截断或清除表变量?

Declare @tableVariable table
(
   id int, 
   value varchar(20)
)


while @start<=@stop

  begin

    insert into @tableVariable(id,value) 
    select id
         , value 
      from xTable 
     where id=@start

   --Use @tableVariable 

   --@tableVariable should be flushed out of 
   -- old values before inserting new  values

    set @start = @start + 1 
  end 

删除所有内容

DELETE FROM @tableVariable

No, you cannot TRUNCATE a table variable since it is not a physical table.不,您不能TRUNCATE表变量,因为它不是物理表。 Deleting it would be faster.删除它会更快。 See this answer from Aaron Bertrand .请参阅Aaron Bertrand 的这个答案。

I'd add to the " technically " correct answer of using DELETE @VariableTable that if you happen to also have an Identity-Field in your @Table Variable (eg i int (1,1) ) and you'd like to re-use this table (even if you re-declare it in a loop) it is still within scope and there it no way to reseed it either.我会添加到使用DELETE @VariableTable的“技术上”正确答案中,如果您碰巧在@Table变量中也有一个 Identity-Field(例如i int (1,1) )并且您想重新 -使用这个表(即使你在循环中重新声明它)它仍然在范围内,也没有办法重新设定它。

See: Table Variable Identity Column请参阅: 表变量标识列

It is best to use #TempTable in these cases - then you may Truncate or use DBCC to reseed.在这些情况下最好使用#TempTable - 然后您可以截断或使用 DBCC 重新播种。
You will reap performance improvements with Truncate and be able to create additional indexes.您将通过 Truncate 获得性能改进,能够创建其他索引。
I think the rule of thumb is, if you're ever going to delete everything using DELETE @VariableTable , then you've introduced a code-smell that says, you should have used #TempTable and TRUNCATE instead.我认为经验法则是,如果您打算使用DELETE @VariableTable删除所有内容,那么您已经引入了一种代码味道,表明您应该使用#TempTableTRUNCATE来代替。

Table variables do not support TRUNCATE syntax - the only way of truncating them is implicitly by letting them fall out of scope.表变量不支持TRUNCATE语法 - 截断它们的唯一方法是让它们超出范围。

Both temporary tables and table variables can be cached when used in stored procedures and the below may well end up with the same table variable being used after truncation rather than an actual drop and create当在存储过程中使用时,临时表和表变量都可以被缓存,下面很可能在截断后使用相同的表变量而不是实际的删除和创建

CREATE PROC dbo.foo @start INT
AS
  BEGIN
      DECLARE @tableVariable TABLE (
        id    INT,
        value VARCHAR(20))

      INSERT INTO @tableVariable
                  (id,
                   value)
      SELECT id,
             value
      FROM   xTable
      WHERE  id = @start;
  --Use @tableVariable 
  END

GO

WHILE @start <= @stop
  BEGIN
      EXEC dbo.foo @start

      SET @start = @start + 1
  END 

Of course a far easier alternative would be to switch to using a #temp table instead as that supports TRUNCATE directly.当然,更简单的替代方法是改用#temp表,因为它直接支持TRUNCATE

DML on both table variables and temp tables writes to the tempdb transaction log.表变量和临时表上的 DML 写入tempdb事务日志。 Whether or not it is worth switching to TRUNCATE rather than DELETE depends on the size of data involved.是否值得切换到TRUNCATE而不是DELETE取决于所涉及的数据大小。 TRUNCATE will just log the page deallocations. TRUNCATE只会记录页面释放。 DELETE will log the actual deleted values. DELETE将记录实际删除的值。 One other difference between the two is that TRUNCATE deallocates the last page from the table and DELETE doesn't.两者之间的另一个区别是TRUNCATE从表中释放最后一页,而DELETE不会。 If only a small quantity of data is inserted and deleted in each loop iteration then the overhead from logging the deleted rows can be less than the overhead from constantly deallocating and reallocating the single page in the table.如果在每次循环迭代中仅插入和删除少量数据,则记录已删除行的开销可能小于不断释放和重新分配表中单页的开销。

Conversely if you will be inserting and deleting large amounts of data on each iteration you may find that TRUNCATE not only makes the operation of deleting all rows more efficient but also can benefit the subsequent insert statement .相反,如果您将在每次迭代中插入和删除大量数据,您可能会发现TRUNCATE不仅使删除所有行的操作更加高效,而且还可以使后续的插入语句受益

--Usage: exec sp_truncateifexists tablename
CREATE PROCEDURE sp_truncateifexists
    @tableVariable nvarchar(200)
AS
BEGIN
    IF EXISTS (
        SELECT 1 
        FROM INFORMATION_SCHEMA.TABLES 
        WHERE TABLE_NAME = @tableVariable )
    BEGIN
        DECLARE @query nvarchar(250)
        SET @query = 'TRUNCATE TABLE ' + @tableVariable 
        EXECUTE (@query)
    END
END
GO

Remember to use #temp tables if you don't need the tables later.如果以后不需要这些表,请记住使用#temp 表

I know this is an old question but i've figured a way to do this.我知道这是一个老问题,但我想出了一种方法来做到这一点。 we had tables with millions of rows and didn't want to delete them due to transaction log space.我们有包含数百万行的表,并且由于事务日志空间而不想删除它们。

Create a procedure that you pass in the table name you want to truncate, the procedure will create another procedure that does the trucate and then deletes the procedures.创建一个过程,传入要截断的表名,该过程将创建另一个执行截断的过程,然后删除这些过程。

USE [My_Database]
GO
/****** Object:  StoredProcedure [dbo].[ClearOutTable_p1]    Script Date: 23/09/2015 09:03:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      Oraclebhoy
-- Create date: 23/09/2015
-- Description: 
-- 
-- removes the content of the table passed in through the parameter
-- =============================================
create procedure [dbo].[ClearOutTable_p1]
@tablename varchar(max)
as

-- CREATE THE TRUNCATE STATEMENT PASSING IN TABLE VARIABLE
    declare @truncatesql varchar(max)
    set @truncatesql = 'truncate table ' + @tablename

-- IF PROCEDURE EXISTS DROP
    if exists (select name from sys.all_objects where name = 'ClearOutTable_TEMP'and type = 'P')
        begin
            drop procedure [dbo].[ClearOutTable_TEMP]
        end

-- CREATE TEMP PROCEDURE
    exec ('create procedure [dbo].[ClearOutTable_TEMP]
    as
    '+@truncatesql+'')

-- EXECUTE THE PROCEDURE
    exec [dbo].[ClearOutTable_TEMP]

-- DROP THE PROCEDURE
    drop procedure [dbo].[ClearOutTable_TEMP]

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM