[英]Incorrect syntax near the keyword 'select' in SQL Server
I am trying to create a trigger that decreminate the number of books based on the value inserted in the other table. 我正在尝试创建一个触发器,该触发器根据插入另一个表中的值来减少书籍的数量。 I am using the following code
我正在使用以下代码
CREATE TRIGGER updateStock
ON dbo.command
AFTER INSERT
AS
BEGIN
UPDATE dbo.book
SET number = number - (SELECT quant FROM inserted)
WHERE NLIV = SELECT NLIV FROM inserted
But I am getting this error: 但我收到此错误:
Incorrect syntax near the keyword 'SELECT'.
关键字“ SELECT”附近的语法不正确。 Incorrect syntax near ')'
')'附近的语法不正确
That's not the right syntax for an UPDATE
if you need to join
with another table. 如果您需要与另一个表
join
,那不是UPDATE
的正确语法。 Try the following: 请尝试以下操作:
CREATE TRIGGER updateStock
ON dbo.command
AFTER INSERT
AS
BEGIN
UPDATE b
SET b.number = b.number - i.quant
FROM dbo.book b
INNER JOIN INSERTED i
ON b.NLIV = i.NLIV;
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.