| Author |
Topic |
|
Mpilo
Yak Posting Veteran
52 Posts |
Posted - 2008-10-02 : 06:22:33
|
| Hi I need a stored procedure that will delete the record in the database after a certain time and save it in another databasePls |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-02 : 06:30:53
|
Oh, wait!Haven't we sent that link to you before? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-02 : 06:30:57
|
If databases are in same serverCREATE PROC ArchiveExpired@period intASINSERT INTO db2.schemaname.tableSELECT *FROM db1.schemaname.tableWHERE DATEDIFF(dd,datefield,GETDATE()) >=@PeriodDELETE FROM db1.schemaname.tableWHERE DATEDIFF(dd,datefield,GETDATE()) >=@PeriodGO |
 |
|
|
Mpilo
Yak Posting Veteran
52 Posts |
Posted - 2008-10-02 : 06:34:34
|
i'm using sqlserver 2005 and i have a column 'datecreated' that take a date you saved ,i want to count maybe 30 days then delete that recordquote: Originally posted by Peso 1) Which version of SQL Server are you using?2) Do you have some datetime column for which you can check "age" of record?http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx E 12°55'05.63"N 56°04'39.26"
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-02 : 06:36:37
|
| [code]CREATE PROC ArchiveExpired@period intASINSERT INTO db2.schemaname.tableSELECT *FROM db1.schemaname.tableWHERE DATEDIFF(dd,datecreated,GETDATE()) >=@PeriodDELETE FROM db1.schemaname.tableWHERE DATEDIFF(dd,datecreated,GETDATE()) >=@PeriodGO[/code]and use it like this[code]EXEC ArchiveExpired 30[/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-02 : 06:39:28
|
[code]CREATE PROCEDURE dbo.uspArchiveMyRecords( @Date DATETIME = NULL)ASSET NOCOUNT ONIF @Date IS NULL SET @Date = DATEDIFF(DAY, 30, @Date) -- Default to 30 daysELSE SET @Date = DATEDIFF(DAY, 0, @Date)BEGIN TRY BEGIN TRANSACTION INSERT TargetTable ( {col list here} ) SELECT {col list here} FROM SourceTable WHERE Col1 < @Date DELETE FROM SourceTable WHERE Col1 < @Date COMMIT TRANSACTIONEND TRYBEGIN CATCH ROLLBACK TRANSACTIONEND CATCH[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Mpilo
Yak Posting Veteran
52 Posts |
Posted - 2008-10-02 : 06:51:31
|
quote: Originally posted by Peso
CREATE PROCEDURE dbo.uspArchiveMyRecords( @Date DATETIME = NULL)ASSET NOCOUNT ONIF @Date IS NULL SET @Date = DATEDIFF(DAY, 30, @Date) -- Default to 30 daysELSE SET @Date = DATEDIFF(DAY, 0, @Date)BEGIN TRY BEGIN TRANSACTION INSERT TargetTable ( {col list here} ) SELECT {col list here} FROM SourceTable WHERE Col1 < @Date DELETE FROM SourceTable WHERE Col1 < @Date COMMIT TRANSACTIONEND TRYBEGIN CATCH ROLLBACK TRANSACTIONEND CATCH E 12°55'05.63"N 56°04'39.26"
|
 |
|
|
Mpilo
Yak Posting Veteran
52 Posts |
Posted - 2008-10-02 : 06:54:35
|
I understand the stored procedure my problem now is i want it to be deleted automatical without entering an date.is it possible?quote: Originally posted by Peso
CREATE PROCEDURE dbo.uspArchiveMyRecords( @Date DATETIME = NULL)ASSET NOCOUNT ONIF @Date IS NULL SET @Date = DATEDIFF(DAY, 30, @Date) -- Default to 30 daysELSE SET @Date = DATEDIFF(DAY, 0, @Date)BEGIN TRY BEGIN TRANSACTION INSERT TargetTable ( {col list here} ) SELECT {col list here} FROM SourceTable WHERE Col1 < @Date DELETE FROM SourceTable WHERE Col1 < @Date COMMIT TRANSACTIONEND TRYBEGIN CATCH ROLLBACK TRANSACTIONEND CATCH E 12°55'05.63"N 56°04'39.26"
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-02 : 06:58:48
|
Yes, as the comment days "Default to 30 days old records if not datevalue is passed to the stored procedure". E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-02 : 07:01:38
|
| You can schedule a job in SQL Server agent to execute this stored procedure with date value passed to it whenever you want. |
 |
|
|
Mpilo
Yak Posting Veteran
52 Posts |
Posted - 2008-10-02 : 07:13:38
|
| Thanks guys you are the best |
 |
|
|
|