Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instead of trigger in SQL Server loses SCOPE_IDENTITY?

Tags:

I have a table where I created an INSTEAD OF trigger to enforce some business rules.

The issue is that when I insert data into this table, SCOPE_IDENTITY() returns a NULL value, rather than the actual inserted identity.

Insert + Scope code

INSERT INTO [dbo].[Payment]([DateFrom], [DateTo], [CustomerId], [AdminId]) VALUES ('2009-01-20', '2009-01-31', 6, 1)  SELECT SCOPE_IDENTITY() 

Trigger:

CREATE TRIGGER [dbo].[TR_Payments_Insert]    ON  [dbo].[Payment]    INSTEAD OF INSERT AS  BEGIN -- SET NOCOUNT ON added to prevent extra result sets from     -- interfering with SELECT statements.     SET NOCOUNT ON;      IF NOT EXISTS(SELECT 1 FROM dbo.Payment p               INNER JOIN Inserted i ON p.CustomerId = i.CustomerId               WHERE (i.DateFrom >= p.DateFrom AND i.DateFrom <= p.DateTo) OR (i.DateTo >= p.DateFrom AND i.DateTo <= p.DateTo)               ) AND NOT EXISTS (SELECT 1 FROM Inserted p               INNER JOIN Inserted i ON p.CustomerId = i.CustomerId               WHERE  (i.DateFrom <> p.DateFrom AND i.DateTo <> p.DateTo) AND                ((i.DateFrom >= p.DateFrom AND i.DateFrom <= p.DateTo) OR (i.DateTo >= p.DateFrom AND i.DateTo <= p.DateTo))               )      BEGIN         INSERT INTO dbo.Payment (DateFrom, DateTo, CustomerId, AdminId)         SELECT DateFrom, DateTo, CustomerId, AdminId         FROM Inserted     END     ELSE     BEGIN             ROLLBACK TRANSACTION     END   END 

The code worked before the creation of this trigger. I am using LINQ to SQL in C#. I don't see a way of changing SCOPE_IDENTITY to @@IDENTITY. How do I make this work?

like image 401
kastermester Avatar asked May 25 '09 22:05

kastermester


People also ask

Why would you use an instead of trigger?

An INSTEAD OF trigger is a trigger that allows you to update data in tables via their view which cannot be modified directly through DML statements. When you issue a DML statement such as INSERT , UPDATE , or DELETE to a non-updatable view, Oracle will issue an error.

Why would Scope_identity return null?

The SCOPE_IDENTITY() function will return the null value if the function is invoked before any INSERT statements into an identity column occur in the scope. Your SQL code would be very helpful.

What is instead of insert trigger?

An INSTEAD OF trigger is a trigger that allows you to skip an INSERT , DELETE , or UPDATE statement to a table or a view and execute other statements defined in the trigger instead. The actual insert, delete, or update operation does not occur at all.

What is the difference between Scope_identity and @@ Identity?

If you insert a row into the table, @@IDENTITY and SCOPE_IDENTITY() return different values. SCOPE_IDENTITY() returns the value from the insert into the user table, whereas @@IDENTITY returns the value from the insert into the replication system table.


1 Answers

Use @@identity instead of scope_identity().

While scope_identity() returns the last created id in the current scope, @@identity returns the last created id in the current session.

The scope_identity() function is normally recommended over the @@identity field, as you usually don't want triggers to interfer with the id, but in this case you do.

like image 106
Guffa Avatar answered Oct 05 '22 20:10

Guffa