简体   繁体   English

每当插入新记录时,如何将列值复制到同一数据库表的其他列?

[英]How to copy a column value to other column of same database table whenever a new record is inserted?

I have the following database table: 我有以下数据库表:

Book {Id(primary Key), Name, TotalNoOfCopies, NoOfCopiesAvailable}

I want that whenever a new record/row is inserted the column NoOfCopiesAvailabe contains the same value as in column TotalNoOfCopies. 我希望每当插入新记录/行时,列NoOfCopiesAvailabe包含的值与列TotalToOfCopies中的值相同。
I tried using the following successful trigger: 我尝试使用以下成功触发器:

for insert
as declare @id nvarchar(10),@name varchar(50),@author varchar(40),@totalNoOfCopies tinyint, @availableNoOfCOpies tinyint;
select @id=i.Id from inserted i;
select @name=i.Name from inserted i;
select @author=i.Author from inserted i;
select @totalNoOfCopies=i.TotalNoOfCopies from inserted i;
select @availableNoOfCopies=i.TotalNoOfCopies from inserted i;
delete from Book where Id=@id;
insert into Book values(@id,@name,@author,@totalNoOfCopies,@availableNoOfCOpies);

But it performs 3 transactions: 但它执行3笔交易:
1.For inserting a new record 1.用于插入新记录
2.For deleting that new record 2.用于删除该新记录
3.For inserting the new record that has NoOfCopiesAvailable=TotalNoOfCopies 3.用于插入具有NoOfCopiesAvailable = TotalNoOfCopies的新记录

For example: 例如:

Insert into Book values('123','Book','Author',5,null)

Messages: 讯息:
1 row(s) affected 受影响1行
1 row(s) affected 受影响1行
1 row(s) affected 受影响1行


I was wondering if there is any way of doing it in a single transaction? 我想知道单笔交易中是否有任何办法?

Do you need an INSTEAD OF INSERT trigger: 您是否需要INSTEAD OF INSERT触发器:

CREATE TRIGGER Book_i ON Book
INSTEAD OF INSERT
AS
BEGIN
    INSERT INTO Book (
        Id,
        Name,
        TotalNoOfCopies, 
        NoOfCopiesAvailable
    ) SELECT
        Id,
        Name,
        TotalNoOfCopies,
        TotalNoOfCopies
    ) FROM
        INSERTED
END
GO

It is clear an elegant. 显然是优雅的。

我宁愿修复表设计,而改用NumberOfCopiesSold,默认情况下可以为0。

暂无
暂无

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

相关问题 为表创建触发器,每当插入或更新新记录并满足条件时,填充同表中的列 - Create a trigger for a table that fills a column in the same table whenever a new record is inserted or updated and meets the condition 当一个表中的列值更改时,应在oracle DB的另一表中插入一条记录 - When value of a column changes in one table , a record should be inserted in other table in oracle DB MySQL 触发器 - 复制新。 插入的数据与现有列值进行比较,如果匹配,则将它们插入其他表 - MySQL Trigger - Copy NEW. inserted data compare to existing column values and then insert them to other table if they match 有没有办法在将新记录插入 oracle sql 表时自动更新列值 - is there a way to automatically update a column value when a new record is inserted into an oracle sql table 如何将两列的组合值复制到同一表中的一列 - How to copy the combined value of two column to one Column in same table 每当插入新记录时,SQL Server 2012 将 DATEDIFF 插入列触发器 - SQL Server 2012 Insert DATEDIFF into column trigger whenever a new record is inserted 更新表上每个新插入的(同一行)记录的列取决于另一个没有触发器和默认约束的列值 - Update a column of each and every newly inserted (same row) record on a table depends on another column value without Trigger and default constraint 仅当更新另一个表上的列或向另一个表添加新记录时,才将记录插入到新的MySQL数据库表中吗? - Insert record into new MySQL Database table only when a Column on another Table is Updated or a New record is added to that other Table? SQL:如何根据同一表中其他列的值创建新列 - SQL: How to create new columns depending on other column's value in the same table 每当在 PLSQL 中插入新行时,更改前一行和当前行中的列的值 - Change the value of a column in previous row and the current row whenever a new row is inserted in PLSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM