Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Delete Table records with Bakup Expand / Collapse
Author
Message
Posted Friday, May 2, 2014 2:37 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 8:02 AM
Points: 227, Visits: 703
Hi Team,

using below query

DELETE FROM Table_name
WHERE Date_column < GETDATE() - 30

am able to delete old records morethan 30 days, but i want to results to be saved in file.

before deleting i want to a craete a file and save the to be deleted records.

Please suggest.

Post #1566956
Posted Friday, May 2, 2014 2:57 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:56 AM
Points: 2,603, Visits: 2,061
There are two ways you can achieve this.

1st Create a table(tblToBeDeleted) for records to be deleted. Insert the data into tblToBeDeleted and once insert is done you can delete the records which are in table tblToBeDeleted.

2nd you can create export data to excel; refer following link:
http://social.msdn.microsoft.com/Forums/en-US/453c9593-a689-4f7e-8364-fa998e266363/how-to-export-sql-data-to-excel-spreadsheet-using-sql-query?forum=transactsql

HTH


---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
Post #1566958
Posted Friday, May 2, 2014 3:21 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 1:22 PM
Points: 1,215, Visits: 3,548
You can use the output clause with the delete


DECLARE @TTABLE TABLE
(
TT_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,TT_VAL INT NOT NULL
);

INSERT INTO @TTABLE(TT_VAL)
VALUES (123),(234),(345),(456),(567);

SELECT * FROM @TTABLE;

DELETE
FROM @TTABLE
OUTPUT deleted.*
WHERE TT_VAL < 300;

SELECT * FROM @TTABLE;

Results
TT_ID       TT_VAL
----------- -----------
1 123
2 234
3 345
4 456
5 567

TT_ID TT_VAL
----------- -----------
1 123
2 234

TT_ID TT_VAL
----------- -----------
3 345
4 456
5 567
Post #1566972
Posted Friday, May 2, 2014 4:11 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:56 AM
Points: 2,603, Visits: 2,061
Great Eirikur, is it possible to send this OUTPUT to excel using this T-sql?

---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
Post #1566974
Posted Friday, May 2, 2014 4:29 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 1:22 PM
Points: 1,215, Visits: 3,548
free_mascot (5/2/2014)
Great Eirikur, is it possible to send this OUTPUT to excel using this T-sql?


Use the output to insert the records into a table and export to excel/csv from there. I added the insert bit to the following code


DECLARE @TTABLE TABLE
(
TT_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,TT_VAL INT NOT NULL
);
DECLARE @TTDELETED TABLE
(
TT_ID INT PRIMARY KEY CLUSTERED NOT NULL
,TT_VAL INT NOT NULL
);

INSERT INTO @TTABLE(TT_VAL)
VALUES (123),(234),(345),(456),(567);

SELECT * FROM @TTABLE;

DELETE
FROM @TTABLE
OUTPUT deleted.* INTO @TTDELETED(TT_ID,TT_VAL)
WHERE TT_VAL < 300;

SELECT * FROM @TTABLE;
SELECT * FROM @TTDELETED;

Post #1566980
Posted Monday, May 5, 2014 3:47 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 1:16 PM
Points: 1,948, Visits: 2,883
I would never use a table variable for this -- if something goes wrong after the DELETEs, but before the output has been processed, the data is gone, with no way to get it back.

Do you have a separate db that you use for export / other utility purposes? If not, you may want to create one. Output the rows to be deleted to a table, then delete the rows.

Process that other table within a transaction, so that the rows must be successfully exported before the export table has its rows deleted.


SQL DBA,SQL Server MVP('07, '08, '09)
"In America, every man is innocent until proven broke!" Brant Parker
Post #1567727
Posted Monday, May 5, 2014 3:53 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 1:22 PM
Points: 1,215, Visits: 3,548
ScottPletcher (5/5/2014)
I would never use a table variable for this -- if something goes wrong after the DELETEs, but before the output has been processed, the data is gone, with no way to get it back.

Do you have a separate db that you use for export / other utility purposes? If not, you may want to create one. Output the rows to be deleted to a table, then delete the rows.

Process that other table within a transaction, so that the rows must be successfully exported before the export table has its rows deleted.


I agree, the table variables are for the sake of providing a sample schema for the demonstration of the functionality, should have been more clear on that!
Post #1567730
Posted Monday, May 5, 2014 4:32 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:05 PM
Points: 36,710, Visits: 31,158
free_mascot (5/2/2014)
Great Eirikur, is it possible to send this OUTPUT to excel using this T-sql?


Why do you need to send it to Excel?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1567736
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse