What Order should I Call @@ROWCOUNT/@@ERROR
I am inserting a number or rows into a table using INSERT with SELECT. After the transaction, I want to store both the @@ROWCOUNT and @@ERROR values into locallay declared variables.
INSERT SubscriberList (PublicationId, SubscriberId)
SELECT @Publicat开发者_开发技巧ionId, S.SubscriberId
FROM Subscribers S
SET @NoRows = @@ROWCOUNT
SET @ErrorCode = @@ERROR
I wasn't sure if this was valid in as much if I call one, will I negate the other?
Set them both at once:
SELECT @NoRows = @@ROWCOUNT, @ErrorCode = @@ERROR
In addition to @JNK's answer...
I never use @@ERROR now because of TRY/CATCH
BEGIN TRY
BEGIN TRAN
INSERT SubscriberList (PublicationId, SubscriberId)
SELECT @PublicationId, S.SubscriberId
FROM Subscribers S
SET @NoRows = @@ROWCOUNT
... do more inserts, updates etc
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
SET @ErrorCode = ERROR_NUMBER()
RAISERROR ...
END CATCH
精彩评论