文件名称:查询误删除数据表的存储过程
文件大小:1KB
文件格式:TXT
更新时间:2013-01-19 08:45:34
存储过程 误删除
第一步:新建存储过程 CREATE PROCEDURE PCreateDeleteTrigger @tableName varchar(128) AS DECLARE @fields sysname,@sqlStr VARCHAR(2048),@log_tableName varchar(128) SET @log_tableName = @tableName+'_DeleteLog' SET @sqlStr = '' SET @sqlStr ='select top 0 * into '+@log_tableName+' from '+@tableName EXEC(@sqlStr) SET @sqlStr = 'ALTER TABLE '+@log_tableName+' drop column id ALTER TABLE '+@log_tableName+' add id int ALTER TABLE '+@log_tableName+' ADD xid int NOT NULL IDENTITY (1, 1) ALTER TABLE '+@log_tableName+' ADD OccurTime datetime NULL ' EXEC(@sqlStr) SET @sqlStr = ''; DECLARE getFields CURSOR FOR SELECT name FROM syscolumns WHERE (id IN (SELECT id FROM sysobjects WHERE xtype = 'u' AND name = @tableName )) and xtype not in (34,35,99) ORDER BY colid OPEN getFields FETCH NEXT FROM getFields INTO @fields WHILE @@fetch_status = 0 BEGIN IF @@fetch_status = -2 CONTINUE BEGIN TRANSACTION SET @sqlStr = @sqlStr + CAST(@fields as nvarchar(128)) + ',' IF @@error = 0 BEGIN COMMIT END ELSE BEGIN ROLLBACK END FETCH NEXT FROM getFields INTO @fields END DEALLOCATE getFields SET @sqlStr ='insert into ' + @log_tableName +'(' + @sqlStr + 'OccurTime) ' + 'select ' + @sqlStr + 'getdate() as OccurTime from deleted' SET @sqlStr = 'CREATE TRIGGER '+@tableName+'DeleteTrigger ON '+@tableName+' AFTER DELETE AS '+@sqlStr EXEC(@sqlStr) GO 第二步:执行存储过程 exec 存储过程名称 表名