SQLite TRIGGER to SQL server conversion.

Jason Webb 21 Reputation points
2022-07-13T12:23:01.967+00:00

What would the SQL server version of the following. Is there an SQL server conversion for the NEW command. Do I have to JOIN the widgetSale table?

CREATE TRIGGER newWidgetSale
ON widgetSale
AFTER INSERT
AS
BEGIN
UPDATE widgetCustomer SET last_order_id = NEW.id WHERE widgetCustomer.id = NEW.customer_id
END

Developer technologies Transact-SQL
{count} votes

Accepted answer
  1. Tom Phillips 17,771 Reputation points
    2022-07-13T13:35:31.703+00:00

    SQL Server uses tables INSERTED/DELETED in triggers.

    See:
    https://learn.microsoft.com/en-us/sql/relational-databases/triggers/use-the-inserted-and-deleted-tables?view=sql-server-ver16
    https://www.mssqltips.com/sqlservertip/2342/understanding-sql-server-inserted-and-deleted-tables-for-dml-triggers/

    UPDATE wc  
    	SET last_order_id = NEW.id   
    FROM widgetCustomer wc  
    	INNER JOIN INSERTED NEW   
    	ON widgetCustomer.id = NEW.customer_id  
      
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.