I have a stored procedure in which I do an insert and retrieve the ID of the row by calling SCOPE_IDENTITY()
...
INSERT INTO [RemoteDB].[dbo].[Table] (StageID, UserID, Date)
VALUES (4, @userID, @date)
SET @id = SCOPE_IDENTITY()
INSERT INTO [RemoteDB].[dbo].[Table2] (ID)
VALUES (@id)
...
Everything works fine when I my 3 databases on the same server, but in production, the one of the databases is remote (so I use a linked server).
The first insert of my query works, but the second doesn't run. I assume this is because of the SCOPE_IDENTITY()
.
My error is:
The OLE DB provider could not INSERT INTO... "The value violated the integrity constraints for the column.".
This means that it's not able to get the @id
of the SCOPE_IDENTITY()
Note that the stored procedure that I execute are not on the remote server, they are on the local one.
What did I do wrong?