Is ROWCOUNT reliable for change checking?

Asked

Viewed 123 times

0

I am mounting a query to be executed via DAPPER/C#, my idea is not to check if the data exists in the table returning the object to the application, but rather, to check directly in the data bank of a single time.

I know it’s possible to use the MARGE, but with it I have the complication of not being able to use the WITH CHANGE_TRACKING_CONTEXT.

That’s why I built my query using ROWCOUNT that returns the amount of line affected and if it is equal to zero do the Insert.

My question is whether it is reliable to use the ROWCOUNT for a situation like this, being that my table may have Rigger and making other business rules including update and Insert in other tables.

WITH CHANGE_TRACKING_CONTEXT( 0x4143544956452D53..)
UPDATE MINHA_TABELA
SET CAMPO2 = 44
   ,CAMPO1 = 1
WHERE ID = 1
IF @@ROWCOUNT = 0
BEGIN
    WITH CHANGE_TRACKING_CONTEXT( 0x4143544956452D53..)
    INSERT INTO MINHA_TABELA (ID, CAMPO1, CAMPO2)
    VALUES (1, 1, 44)
END 
  • 1

    Is yes...........

  • @Virgilionovic, even if the table has Rigger, knows whether it returns the affected rows of a Rigger or only the same table?

  • From what I understand you run via Dapper if yes gives now Rigger have no way to check and this is not in your question

  • Yes, I ended up not mentioning the part of Rigger even because I think it was a little obvious that a table has it. but I will edit the question.

  • 1

    according to the official documentation https://docs.microsoft.com/en-us/sql/t-sql/functions/rowcount-transact-sql?view=sql-server-ver15 "Returns the number of Rows affected by the last statement" i.e., from the last command, then an update and then pick up the value of rowcount will return the changed records in that command, now if a Trigger makes changes, it is already another command

  • 1

    It is not at all obvious that any table has triggers.

  • @bfavaretto for me is, but I changed the question.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.