Mysql 在另一个表中存储表的更新历史记录的过程
我有两个表PersonId,Name,Address,Mobile和PersonLog,Id,Name,Address,Mobile,FK_PersonId。我正在尝试将旧数据存储在PersonLog和要更新的Person中 这是我的过程,但它只是更新Person,而不是将Person中选定(编辑)的数据存储到PersonLog中:Mysql 在另一个表中存储表的更新历史记录的过程,mysql,sql,sql-server,sql-server-2012,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(
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
有什么帮助吗?我可能会建议这些更改。。。但我不知道你到底想要什么。或者你的数据 我强烈建议您在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)主键
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
在select语句中将PersonLog与Person表联接时,该过程不起作用。由于该表最初为空,因此PersonLog.FK_PersonId=Person.PersonId的条件为false,并且不会插入任何内容(因为FK_PersonId为NULL)
我看不出连接这两个表以在日志表中插入记录的任何目的。只需删除连接条件并将where子句包含为Person.PersonId=@PersonId您需要在
插入中添加where
。。。选择…
,它确实在哪里person.personid=@personid
可能。。。到底怎么了?(我还会在该表中添加日期时间,以便您知道更改发生的时间,只是因为)您不工作是什么意思?查询是否抛出错误、生成错误结果或不执行任何操作?我们无法访问您的表。因此,我们无法查明发生了什么。我想你在发布这些问题之前应该考虑一下。我正在尝试更新个人数据,并将旧记录保存在PersonLog中,以便查看第三次做了哪些更改。