Is there a way to access inserted
and deleted
tables using a stored procedure or a function WITHOUT using or passing values from a trigger.
Ex:
INSERT INTO dbo.Table (ID)
VALUES (1)
SELECT * FROM inserted -> This raises an error
Is there a way to do something like this in order to capture the inserted value?
I was able to do this to a query which uses the MERGE
command as below.
MERGE Products AS TARGET
USING UpdatedProducts AS SOURCE ON (TARGET.ProductID = SOURCE.ProductID)
WHEN MATCHED AND TARGET.ProductName <> SOURCE.ProductName
OR TARGET.Rate <> SOURCE.Rate
THEN
UPDATE SET TARGET.ProductName = SOURCE.ProductName,
TARGET.Rate = SOURCE.Rate
WHEN NOT MATCHED BY TARGET
THEN
INSERT (ProductID, ProductName, Rate)
VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate)
WHEN NOT MATCHED BY SOURCE
THEN
DELETE
OUTPUT
$action,
DELETED.ProductID AS TargetProductID,
DELETED.ProductName AS TargetProductName,
DELETED.Rate AS TargetRate,
INSERTED.ProductID AS SourceProductID,
INSERTED.ProductName AS SourceProductName,
INSERTED.Rate AS SourceRate;
Is there a way to access inserted
and deleted
tables for simple SQL queries?