使用触发器从其他 table 自动插入
Using trigger to insert automatically from other table
我需要在 SQL 服务器数据库中创建触发器,以便在用户在 table 中插入一行后,它会自动从另一个 table by bill 导入账单号在两个 table 中都记入帐户并将其插入我当前 table 的 userCode 列中,我的查询是:
ALTER TRIGGER [dbo].[payTrigger]
ON [dbo].[Payment] AFTER INSERT As
BEGIN
update Payment
set userCode = (
select Bill.userCode
from Bill
inner join Payment on Bill.BillAccount = Payment.BillAccount
)
问题是:查询returns所有列,如何让它导入与userCode相关的特定字段,当我尝试插入新行时,出现此错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Bill
table 中有很多不同的 userCode
值与相同的 BillAccount
匹配。根据某些标准,您必须 select 其中之一 - 例如,您可以在子查询中使用 select MIN(Bill.userCode)
获得最小值。
要仅更新新插入的行,您必须添加 WHERE Payment.ID = INSERTED.ID
- 如果没有它,将更新所有 table 行。
我需要在 SQL 服务器数据库中创建触发器,以便在用户在 table 中插入一行后,它会自动从另一个 table by bill 导入账单号在两个 table 中都记入帐户并将其插入我当前 table 的 userCode 列中,我的查询是:
ALTER TRIGGER [dbo].[payTrigger]
ON [dbo].[Payment] AFTER INSERT As
BEGIN
update Payment
set userCode = (
select Bill.userCode
from Bill
inner join Payment on Bill.BillAccount = Payment.BillAccount
)
问题是:查询returns所有列,如何让它导入与userCode相关的特定字段,当我尝试插入新行时,出现此错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Bill
table 中有很多不同的userCode
值与相同的BillAccount
匹配。根据某些标准,您必须 select 其中之一 - 例如,您可以在子查询中使用select MIN(Bill.userCode)
获得最小值。要仅更新新插入的行,您必须添加
WHERE Payment.ID = INSERTED.ID
- 如果没有它,将更新所有 table 行。