我有2列Quantity和QuantityOrder,如果我是我想要订购的客户,那么在数据库中,数量列必须减去值A和QuantityOrder'll a a value A 例: 数量-1 QuantityOrder +1 我不知道如何写插入声明,请帮帮我
答案 0 :(得分:1)
好。使用评论中的其他信息,这将在SQL 2012上运行:
首先为您的数据模型提供一些急救:
CREATE TABLE [Orders] (
CustomerId INT,
ProductId INT,
Quantity INT,
OrderDate datetime2 default GetDate(),
EnteredBy SYSNAME default original_login()
)
GO
然后交易代码为:
BEGIN TRANSACTION
DECLARE @Quantity INT
DECLARE @CustomerId INT
DECLARE @ProductId INT
INSERT INTO Orders (customerId,productId,quantity)
VALUES (@CustomerId,@ProductId,@Quantity)
UPDATE Customer
SET quantityOrder = QuantityOrder + @Quantity
WHERE CustomerId = @CustomerId
UPDATE product
SET quantity = quantity - @Quantity
WHERE productId = @ProductId
COMMIT TRANSACTION