I keep getting a conversion error, but what I need to do is add data from one table, and change the DateAdded column to a DateUpdated column in the new table. The data from the original table is:
ProductID (PK, int, NOT NULL)
CategoryID (FK, int, NULL)
ProductCode (varchar(10), NOT NULL)
ProductName(varchar(255), NOT NULL)
Description(Text, NOT NULL)
ListPrice(money, NOT NULL)
DiscountPercent(money, NOT NULL)
DateAdded(datetime, NULL)
I created a new table:
CREATE TABLE ProductsAudit
(AuditID int NOT NULL PRIMARY KEY,
ProductID int NOT NULL,
CategoryID int NULL,
ProductCode varchar(10) NOT NULL,
ProductName varchar(255) NOT NULL,
DateUpdated datetime NULL,
ListPrice money NOT NULL,
DiscountPercent money NOT NULL);
Now I need to add 1 column and change the name of one column, here is what I did:
USE MyGuitarShop;
GO
IF OBJECT_ID('Products_UPDATE') IS NOT NULL
DROP TRIGGER Products_UPDATE
GO
CREATE TRIGGER Products_UPDATE
ON Products
AFTER INSERT, UPDATE
AS
INSERT ProductsAudit
SELECT AuditID, Products.ProductID, Products.CategoryID, Products.ProductCode,
Products.ProductName,Products.ListPrice, Products.DiscountPercent, DateAdded AS DateUpdated
FROM Products JOIN ProductsAudit
ON ProductsAudit.AuditID = (SELECT AuditID FROM inserted)
I need to change the DateAdded column to the DateUpdated name, but insert all of the data from the original
INSERT INTO ... SELECT ..... Also: your trigger has a fundamental flaw in that you expect it to be called for each row - this is NOT the case in SQL Server, and theInsertedpseudo table can (and will!) contain multiple rows - so which one are you selecting in your(SELECT ProductID FROM inserted)statement? It's undefined - plus you're ignoring all the other rows inInserted, too!