在另一个表中存储表的更新历史记录的过程

时间:2014-10-05 23:24:31

标签: mysql sql sql-server sql-server-2012

我有两个表PersonId,Name,Address,Mobile和PersonLog,包含Id,Name,Address,Mobile,FK_PersonId。我正在尝试将旧数据存储在PersonLog和Person中进行更新。

这是我的程序,但它只是更新Person而不是将所选(已编辑)的数据从Person存储到PersonLog中:

CREATE PROCEDURE [dbo].[UpdateInsertPerson]
(
@PersonId int,
@PersonName nvarchar(40),
@Address nvarchar(60),
@Mobile nvarchar(15)
)

AS

BEGIN

INSERT INTO
dbo.PersonLog(PersonName, Address, Mobile, FK_PersonId)
SELECT
Person.PersonId, Person.PersonName, Person.Address, Person.Mobile
FROM
dbo.Person JOIN dbo.PersonLog ON PersonLog.FK_PersonId = Person.PersonId;

UPDATE
dbo.Person
SET
PersonName = @PersonName,
Address = @Address,
Mobile = @Mobile
WHERE
PersonId = @PersonID;

END

任何帮助?

2 个答案:

答案 0 :(得分:1)

我可能会建议这些改变......但我不知道你到底发生了什么。或者您的数据。

我强烈建议在PersonLog表中添加修改日期....

alter table PersonLog add Modified_DT datetime default getdate()

这段代码对我有用......

CREATE PROCEDURE UpdateInsertPerson (
    @PersonId int,
    @PersonName nvarchar(40) = null,
    @Address nvarchar(60)    = null,
    @Mobile nvarchar(15)     = null
)
AS
BEGIN 

    INSERT INTO PersonLog (FK_PersonId, PersonName, Address, Mobile)
          SELECT Person.PersonID, 
                 Person.PersonName, 
                 Person.Address, 
                 Person.Mobile 
          FROM   Person  
          WHERE  Person.PersonId=@PersonID; 

    UPDATE Person
       SET PersonName = case when @PersonName is not null then @PersonName else PersonName end,
           Address    = case when @Address    is not null then @Address    else Address end,
           Mobile     = case when @Mobile     is not null then @Mobile     else Mobile end 
     WHERE PersonId   = @PersonID;   

END


insert into Person values (1,'kim','123 main st','555-555-5555'); 

exec UpdateInsertPerson 1,'kim';
exec UpdateInsertPerson 1,'ryan';
exec UpdateInsertPerson 1,'taco';


select * from personlog 

select * from Person 

这种方式不会插入一个全新的人。这种方式将...为此工作,您的Person.PersonID必须设置如下:PersonID int IDENTITY(1,1) primary key

ALTER PROCEDURE UpdateInsertPerson (
    @PersonId int            = null,
    @PersonName nvarchar(40) = null,
    @Address nvarchar(60)    = null,
    @Mobile nvarchar(15)     = null
)
AS
BEGIN 

    INSERT INTO PersonLog (FK_PersonId, PersonName, Address, Mobile)
          SELECT Person.PersonID, 
                 Person.PersonName, 
                 Person.Address, 
                 Person.Mobile 
          FROM   Person  
          WHERE  Person.PersonId=@PersonID; 

    UPDATE Person
       SET PersonName = case when @PersonName is not null then @PersonName else PersonName end,
           Address    = case when @Address    is not null then @Address    else Address end,
           Mobile     = case when @Mobile     is not null then @Mobile     else Mobile end 
     WHERE PersonId   = @PersonID;   

    -- this inserts into Person if they didn't already exist
    IF @@ROWCOUNT = 0
    BEGIN
      INSERT Person (PersonName, Address, Mobile) VALUES (@PersonName, @Address, @Mobile); 
    END

END

答案 1 :(得分:0)

由于您在select语句中使用Person表加入PersonLog,因此该过程无效。最初此表为空,条件PersonLog.FK_PersonId = Person.PersonId为false且未插入任何内容(因为FK_PersonId为NULL) 我没有看到加入2个表以在Log表中插入记录的任何目的。只需删除连接条件并将where子句包含为Person.PersonId=@PersonID