HOWTO: Redesign sql table using Insert Select technique

My require­ment was to totally redesign the table when i couldn’t just per­form delete and update tasks on it. In this exam­ple i cre­ate new table based on old table’s fil­tered result set.

real life example:

My client had a Cos­tumerDeals table with large amount of records in it, each time a cos­tumer logged in to check his deals this large table was looped though. So my idea was to cre­ate a trig­ger (see sched­ule MSSQL query task) that will move (insert into new table and then delete) cos­tumers deals which are older than 6 month to CostumerDeals_old table so the records amount decreased in 90% which made faster responses but also made me to pro­gram a trig­ger for removal of older than 6 month user deal and in case user returns to get his deal records back. It was time con­sum­ing but worth it.

code :

use dbName
insert into tableA (colAA,colAB,colAC.....)
select colBA,colBB,colBC.... from tableB where dateadd(mm,6,dealDate)<getdate()
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • LinkedIn
  • StumbleUpon
  • Technorati
  • Live
  • PDF

Tags: sql

Leave a comment