简体   繁体   English

删除SQL Server 2005数据库中的表记录并保留已删除的记录

[英]deleting table records in SQL server 2005 database and keep the deleted records

I am using asp.NET C# and I have a table in SQL server 2005 database. 我正在使用asp.NET C#,并且在SQL Server 2005数据库中有一个表。 Once a student clicks a specific button in the application interface, the student id that will be taken from the session should be stored in the StudentID attribute. 一旦学生单击应用程序界面中的特定按钮,将从会话中获取的学生ID应该存储在StudentID属性中。 At a specific time (for example: at 9:00 AM) I want to delete the table records automatically. 我想在特定时间(例如:9:00 AM)自动删除表记录。 It will be better if I could be able to archive these deleted records somewhere. 如果我能够将这些已删除的记录归档在某个地方会更好。 I searched in Google and I found that I have to make a log trigger, but I am not sure if it will be the best solution in my situation where I want to delete the table records twice a day (in two different hours)!! 我在Google中进行搜索,发现我必须创建一个日志触发器,但是我不确定这是否是我每天两次(在两个小时内)删除表记录的最佳解决方案!

Actually I never use the trigger but I read that it used to delete records in someway. 实际上,我从未使用过触发器,但我读到它曾经以某种方式用于删除记录。

Any help regarding deleting the table records in SQL server 2005 database will be appreciated.. 有关在SQL Server 2005数据库中删除表记录的任何帮助,将不胜感激。

Thanks. 谢谢。

A regular SQL trigger should work fine: 常规的SQL触发器应该可以正常工作:

create trigger FooDeleteTrigger on Foo
after delete
as
insert FooArchive (...columns...)
select ...columns...
from deleted

Alternatively, all in one command: 或者,全部合一命令:

delete from Foo
output deleted.col1, deleted.col2, ...
into FooArchive
--where clause as needed, etc

Write a stored procedure to delete the records from the table and in the same SP write statements to insert the deleted record in your archive table. 编写存储过程以从表中删除记录,并在同一SP写入语句中将删除的记录插入存档表中。 You can use deleted keyword to access the deleted rows from the table. 您可以使用delete关键字来访问表中已删除的行。 Now once you are done writing the SP create a job in SQL Server on this SP and schedule it as per your requirement. 现在,完成SP的编写后,请在该SP上的SQL Server中创建一个作业,并根据您的要求进行调度。

Alternatively you can only put the deletion logic in the stored procedure and move the insertion-to-archive-table logic to trigger developed on the main table. 或者,您只能将删除逻辑放在存储过程中,然后将插入到归档表的逻辑移动到触发在主表上开发的逻辑。

Check this out for your reference: http://technet.microsoft.com/en-us/library/ms177564.aspx 签出以供参考: http : //technet.microsoft.com/zh-cn/library/ms177564.aspx

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

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