开发者

How to work with the current record being created in an SQL trigger

开发者 https://www.devze.com 2023-03-13 02:58 出处:网络
I dont know if I am doing this correctly but here goes... I want to write an SQL trigger that looks at the current record being inserted and reacts to it but my question is; how do I work with data i

I dont know if I am doing this correctly but here goes...

I want to write an SQL trigger that looks at the current record being inserted and reacts to it but my question is; how do I work with data in this record?

For example, if a new order was being inserted in to the orders table, I want to be able to get 开发者_JS百科the customer name from that order to use in some logic (lets say an email for now).

I had thought about using a SELECT statment that returned the TOP 1 and ORDER BY DESC but this seems a bit sloppy to me?

Any advice would be greatly received!

Thanks in advance.


For MS SQL Server, you need to query the inserted table to get the records that are being inserted.

Although, the inserted table will include all rows being inserted for that transaction. Processing rows on a row by row isn't generally advised in SQL Server. You should be using a set based approach.

Obviously, if only one row is being inserted then there will only one row in the inserted table.

0

精彩评论

暂无评论...
验证码 换一张
取 消