我编写了代码来创建表并将Products表中的信息插入到ProductsAudit表中。我的问题是,当我将数据插入Products表时,我收到一个错误,告诉我DateUpdated列不允许NULL值。我试图插入GETDATE()并且无法找出造成问题的原因。
USE MyGuitarshop
GO
CREATE TABLE ProductsAudit(
AuditID int PRIMARY KEY IDENTITY,
ProductID int NOT NULL,
CategoryID int NOT NULL,
ProductCode varchar(10) NOT NULL,
ProductName varchar(255)NOT NULL,
ListPrice money NOT NULL,
DiscountPercent money NOT NULL,
DateUpdated datetime2 NOT NULL);
GO
CREATE TRIGGER ProductsAudit_UPDATE
ON Products
AFTER INSERT, UPDATE
AS
INSERT INTO
ProductsAudit (ProductID, CategoryID, ProductCode, ProductName, ListPrice, DiscountPercent, DateUpdated)
SELECT
Products.ProductID,
Products.CategoryID,
Products.ProductCode,
Products.ProductName,
Products.ListPrice,
Products.DiscountPercent,
DateAdded AS DateUpdated
FROM Products
GO
INSERT INTO
Products (CategoryID, ProductCode, ProductName, Description, ListPrice,DiscountPercent, DateAdded)
Values
(1, '229715', 'Sushi', 'Great food for hungry students', 9.99, 12, GETDATE());
GO
USE MyGuitarShop
SELECT * FROM ProductsAudit
答案 0 :(得分:1)
我认为您可能希望AuditID
成为identity
列:
CREATE TABLE ProductsAudit(
AuditID int IDENTITY PRIMARY KEY,
完成后,您无需在插入中指定该列。您也可能打算将getdate()
用于DateUpdated
,尤其是因为您在DateAdded
的{{1}}列中插入了null。
Product