DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
 
 
SITE SEARCH SQLUSA.com VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
How to purge data into history table?

Execute the following Microsoft SQL Server T-SQL script in Management Studio Query Editor to create tables for testing and demonstrate the purge into history table.

 

-- Create test tables with SELECT INTO

SELECT ProductID=CONVERT(int,ProductID), ProdName=Name, ProductNumber,

       ListPrice, Color

INTO tempdb.dbo.NewProduct

FROM AdventureWorks2008.Production.Product

-- (504 row(s) affected)

SELECT top (0) * INTO tempdb.dbo.ProductHistory

FROM tempdb.dbo.NewProduct

GO

-- (0 row(s) affected)

/**** DELETING & TRANSFERRING to History table in one statement ****/

-- The OUTPUT clause

DELETE tempdb.dbo.NewProduct

  OUTPUT deleted.* INTO tempdb.dbo.ProductHistory

WHERE ProductID > 800

GO

-- (199 row(s) affected)

SELECT * FROM tempdb.dbo.NewProduct

-- (305 row(s) affected)

SELECT * FROM tempdb.dbo.ProductHistory

-- (199 row(s) affected)

drop table tempdb.dbo.NewProduct

drop table tempdb.dbo.ProductHistory

------------

Related articles:

http://www.sqlusa.com/bestpractices2005/administration/deleteinbatches/

http://www.sqlusa.com/bestpractices2008/output/

 

Exam Prep 70-461
SQL 2016 DESIGN & PROGRAMMING
 
 
 
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE