开发者

Way to copy row content to different table without having to type all the columns in SQL Server

I have a table with some columns. For archive purposes I usually create second table that looks exactly the same but I add additional Primary Key and Username/DateTime. After every edit I call procedure that looks more or less like that:

CREATE PROCEDURE [dbo].[sto_archiwizujPodmiot]
    @varPodmiotID INT, @varUzytkownik NVARCHAR(50), @varUzytkownikData DATETIME
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

INSERT INTO [dbo].[PodmiotyHistoria]
           ([PodmiotID]
           ,[PodmiotOsobaNazwisko]
           ,[PodmiotOsobaImie]
           ,[PodmiotUzytkownik]
           ,[PodmiotUzytkownikData])
 SELECT [PodmiotID]
      ,[PodmiotOsobaNazwisko]
      ,[PodmiotOsobaImie]
      ,@varUzytkownik
      ,@varUzytkownikData
  FROM [dbo].[Podmioty]
WHERE [PodmiotID] = @varPodmiotID
END

I create such procedure for every single table that I need to have archive for. For example if someone is going to edit some fields before the update happens I call this procedure. Problem is it's a bit of work to create this for every single table, not much but still. Is there a way to make more generalized procedure that would let me do the same but I could call it for every single table just by giving para开发者_开发百科meters like TableName, ArchiveTableName, Username, DateTime or it's what I am supposed to do every time ?


Auditing tables in this way is best done with triggers.

You still need to create a trigger for each such table.

I have found the following article that has scripts that will auto generate the trigger code for the supplied table/s.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜