@@ROWCOUNT returning 1 when no UPDATE made












0















I have some SQL within a stored procedure where I am updating a table based on another SELECT statement from a temp table (code below).



SET NOCOUNT ON
DECLARE @RowCount int

UPDATE TABLEX SET
TRA = ISNULL (ir.DcTra, DCBASIC.TRA),
TRD = ISNULL(CAST(NULLIF(REPLACE(ir.DcTRD, '-', ''), '') AS datetime), DCBASIC.TRD),
LSINC = ISNULL(ir.DcLsInc, DCBASIC.LSINC),
REVSWOVR = ISNULL(ir.DcRevswovr, DCBASIC.REVSWOVR) FROM #TempData ir WHERE TABLEX.MEMBNO = ir.IntMembNo

SET @RowCount = @@ROWCOUNT


The @RowCount variable is being set to 1.



The SELECT of the #TempData table returns no rows and no rows in the TABLEX table are updated (or even exist) with the MembNo (I have added SELECT statements within the sp to debug and they confirm this)



Why is @RowCount being set to 1?










share|improve this question

























  • How do you know that no update was performed? Add: SELECT COUNT(*) FROM #TempData.

    – usr
    Apr 16 '14 at 7:48











  • @usr - as I stated in my post, "(I have added SELECT statements within the sp to debug and they confirm this)"

    – Mike
    Apr 16 '14 at 8:07











  • Ok, did not see that statement. Are there triggers? To be clear: It is extremely unlikely that you have found a bug in SQL Server so the bug is with you.; Create a after-trigger that just throws an exception if any rows are incoming for update. That way you can make sure that there really is no update.

    – usr
    Apr 16 '14 at 8:48











  • Can you execute next query and give us a result select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX)

    – veljasije
    Apr 16 '14 at 8:56











  • @veljasije - select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX) reurns ZERO

    – Mike
    Apr 16 '14 at 12:52


















0















I have some SQL within a stored procedure where I am updating a table based on another SELECT statement from a temp table (code below).



SET NOCOUNT ON
DECLARE @RowCount int

UPDATE TABLEX SET
TRA = ISNULL (ir.DcTra, DCBASIC.TRA),
TRD = ISNULL(CAST(NULLIF(REPLACE(ir.DcTRD, '-', ''), '') AS datetime), DCBASIC.TRD),
LSINC = ISNULL(ir.DcLsInc, DCBASIC.LSINC),
REVSWOVR = ISNULL(ir.DcRevswovr, DCBASIC.REVSWOVR) FROM #TempData ir WHERE TABLEX.MEMBNO = ir.IntMembNo

SET @RowCount = @@ROWCOUNT


The @RowCount variable is being set to 1.



The SELECT of the #TempData table returns no rows and no rows in the TABLEX table are updated (or even exist) with the MembNo (I have added SELECT statements within the sp to debug and they confirm this)



Why is @RowCount being set to 1?










share|improve this question

























  • How do you know that no update was performed? Add: SELECT COUNT(*) FROM #TempData.

    – usr
    Apr 16 '14 at 7:48











  • @usr - as I stated in my post, "(I have added SELECT statements within the sp to debug and they confirm this)"

    – Mike
    Apr 16 '14 at 8:07











  • Ok, did not see that statement. Are there triggers? To be clear: It is extremely unlikely that you have found a bug in SQL Server so the bug is with you.; Create a after-trigger that just throws an exception if any rows are incoming for update. That way you can make sure that there really is no update.

    – usr
    Apr 16 '14 at 8:48











  • Can you execute next query and give us a result select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX)

    – veljasije
    Apr 16 '14 at 8:56











  • @veljasije - select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX) reurns ZERO

    – Mike
    Apr 16 '14 at 12:52
















0












0








0








I have some SQL within a stored procedure where I am updating a table based on another SELECT statement from a temp table (code below).



SET NOCOUNT ON
DECLARE @RowCount int

UPDATE TABLEX SET
TRA = ISNULL (ir.DcTra, DCBASIC.TRA),
TRD = ISNULL(CAST(NULLIF(REPLACE(ir.DcTRD, '-', ''), '') AS datetime), DCBASIC.TRD),
LSINC = ISNULL(ir.DcLsInc, DCBASIC.LSINC),
REVSWOVR = ISNULL(ir.DcRevswovr, DCBASIC.REVSWOVR) FROM #TempData ir WHERE TABLEX.MEMBNO = ir.IntMembNo

SET @RowCount = @@ROWCOUNT


The @RowCount variable is being set to 1.



The SELECT of the #TempData table returns no rows and no rows in the TABLEX table are updated (or even exist) with the MembNo (I have added SELECT statements within the sp to debug and they confirm this)



Why is @RowCount being set to 1?










share|improve this question
















I have some SQL within a stored procedure where I am updating a table based on another SELECT statement from a temp table (code below).



SET NOCOUNT ON
DECLARE @RowCount int

UPDATE TABLEX SET
TRA = ISNULL (ir.DcTra, DCBASIC.TRA),
TRD = ISNULL(CAST(NULLIF(REPLACE(ir.DcTRD, '-', ''), '') AS datetime), DCBASIC.TRD),
LSINC = ISNULL(ir.DcLsInc, DCBASIC.LSINC),
REVSWOVR = ISNULL(ir.DcRevswovr, DCBASIC.REVSWOVR) FROM #TempData ir WHERE TABLEX.MEMBNO = ir.IntMembNo

SET @RowCount = @@ROWCOUNT


The @RowCount variable is being set to 1.



The SELECT of the #TempData table returns no rows and no rows in the TABLEX table are updated (or even exist) with the MembNo (I have added SELECT statements within the sp to debug and they confirm this)



Why is @RowCount being set to 1?







sql-server sql-server-2008 tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 16 '14 at 20:13









a_horse_with_no_name

300k46458550




300k46458550










asked Apr 16 '14 at 7:31









MikeMike

1,08141642




1,08141642













  • How do you know that no update was performed? Add: SELECT COUNT(*) FROM #TempData.

    – usr
    Apr 16 '14 at 7:48











  • @usr - as I stated in my post, "(I have added SELECT statements within the sp to debug and they confirm this)"

    – Mike
    Apr 16 '14 at 8:07











  • Ok, did not see that statement. Are there triggers? To be clear: It is extremely unlikely that you have found a bug in SQL Server so the bug is with you.; Create a after-trigger that just throws an exception if any rows are incoming for update. That way you can make sure that there really is no update.

    – usr
    Apr 16 '14 at 8:48











  • Can you execute next query and give us a result select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX)

    – veljasije
    Apr 16 '14 at 8:56











  • @veljasije - select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX) reurns ZERO

    – Mike
    Apr 16 '14 at 12:52





















  • How do you know that no update was performed? Add: SELECT COUNT(*) FROM #TempData.

    – usr
    Apr 16 '14 at 7:48











  • @usr - as I stated in my post, "(I have added SELECT statements within the sp to debug and they confirm this)"

    – Mike
    Apr 16 '14 at 8:07











  • Ok, did not see that statement. Are there triggers? To be clear: It is extremely unlikely that you have found a bug in SQL Server so the bug is with you.; Create a after-trigger that just throws an exception if any rows are incoming for update. That way you can make sure that there really is no update.

    – usr
    Apr 16 '14 at 8:48











  • Can you execute next query and give us a result select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX)

    – veljasije
    Apr 16 '14 at 8:56











  • @veljasije - select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX) reurns ZERO

    – Mike
    Apr 16 '14 at 12:52



















How do you know that no update was performed? Add: SELECT COUNT(*) FROM #TempData.

– usr
Apr 16 '14 at 7:48





How do you know that no update was performed? Add: SELECT COUNT(*) FROM #TempData.

– usr
Apr 16 '14 at 7:48













@usr - as I stated in my post, "(I have added SELECT statements within the sp to debug and they confirm this)"

– Mike
Apr 16 '14 at 8:07





@usr - as I stated in my post, "(I have added SELECT statements within the sp to debug and they confirm this)"

– Mike
Apr 16 '14 at 8:07













Ok, did not see that statement. Are there triggers? To be clear: It is extremely unlikely that you have found a bug in SQL Server so the bug is with you.; Create a after-trigger that just throws an exception if any rows are incoming for update. That way you can make sure that there really is no update.

– usr
Apr 16 '14 at 8:48





Ok, did not see that statement. Are there triggers? To be clear: It is extremely unlikely that you have found a bug in SQL Server so the bug is with you.; Create a after-trigger that just throws an exception if any rows are incoming for update. That way you can make sure that there really is no update.

– usr
Apr 16 '14 at 8:48













Can you execute next query and give us a result select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX)

– veljasije
Apr 16 '14 at 8:56





Can you execute next query and give us a result select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX)

– veljasije
Apr 16 '14 at 8:56













@veljasije - select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX) reurns ZERO

– Mike
Apr 16 '14 at 12:52







@veljasije - select count(*) from #TempData where IntMembNo in (select MEMBNO from TABLEX) reurns ZERO

– Mike
Apr 16 '14 at 12:52














2 Answers
2






active

oldest

votes


















3














Here is an explanation:



Statements that make a simple assignment always set the @@ROWCOUNT value to 1.


More information you can find here:



@@ROWCOUNT



My example:



CREATE DATABASE FirstDB
GO

USE FirstDB;
GO

CREATE TABLE Person (
personId INT IDENTITY PRIMARY KEY,
firstName varchar(20) ,
lastName varchar(20) ,
age int
)

INSERT INTO dbo.Person (firstName, lastName, age)
VALUES ('Nick', 'Smith', 30),
('Jack', 'South', 25),
('Garry', 'Perth', 20)


CREATE TABLE PersonAge (
personAgeId INT IDENTITY PRIMARY KEY ,
personId INT ,
newAge varchar(10)
)

INSERT INTO dbo.PersonAge(personId, newAge)
VALUES (1, 60),
(2, 65),
(3, 70)

ALTER TABLE dbo.PersonAge
ADD CONSTRAINT FK_PersonAgePerson FOREIGN KEY (personId)
REFERENCES dbo.Person (personId)


And then example of query:



USE FirstDB;
GO

SET NOCOUNT ON;
DECLARE @row int;

UPDATE Person
SET age = 40
FROM dbo.Person as p join dbo.PersonAge as p1
ON p.personId = p1.personId
WHERE p.age = 60

SET @row = @@ROWCOUNT
SELECT @row


I create an UPDATE query where none of rows will be affected.



At the end @row consist 0 value.






share|improve this answer





















  • 1





    Thanks @veljasije - which part of my UPDATE is a "simple assignment"? "Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'."

    – Mike
    Apr 16 '14 at 7:42











  • Thanks @veljasije - there was a rogue SET @Err = @@ERROR between my UPDATE statement and the setting of my variable. Amazing when someone else does code that works - it focussed your mind! Thanks again

    – Mike
    Apr 17 '14 at 8:13



















0














Here is another example, using INSERT and DELETE--



DECLARE @deletedRows INT = 0;
SELECT @deletedRows = @@ROWCOUNT; --no previous DML statement
SELECT @deletedRows; --@@ROWCOUNT = 1 for a simple assignment
GO

DROP TABLE IF EXISTS #Test;
GO
CREATE TABLE #Test (ID INT IDENTITY, CurrentDate DATETIME DEFAULT GETDATE());
GO

INSERT #Test DEFAULT VALUES; --INSERT a single row
DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
SELECT @deletedRows;
GO

DELETE FROM #Test WHERE 1=2; --no rows deleted
DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
SELECT @deletedRows;
GO

DELETE TOP (1) t FROM #Test t WHERE 1=1; --1 row deleted
DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
SELECT @deletedRows;
GO

DELETE TOP (1) t FROM #Test t WHERE 1=1; --no rows left to delete
DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
SELECT @deletedRows;
GO





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23102805%2frowcount-returning-1-when-no-update-made%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Here is an explanation:



    Statements that make a simple assignment always set the @@ROWCOUNT value to 1.


    More information you can find here:



    @@ROWCOUNT



    My example:



    CREATE DATABASE FirstDB
    GO

    USE FirstDB;
    GO

    CREATE TABLE Person (
    personId INT IDENTITY PRIMARY KEY,
    firstName varchar(20) ,
    lastName varchar(20) ,
    age int
    )

    INSERT INTO dbo.Person (firstName, lastName, age)
    VALUES ('Nick', 'Smith', 30),
    ('Jack', 'South', 25),
    ('Garry', 'Perth', 20)


    CREATE TABLE PersonAge (
    personAgeId INT IDENTITY PRIMARY KEY ,
    personId INT ,
    newAge varchar(10)
    )

    INSERT INTO dbo.PersonAge(personId, newAge)
    VALUES (1, 60),
    (2, 65),
    (3, 70)

    ALTER TABLE dbo.PersonAge
    ADD CONSTRAINT FK_PersonAgePerson FOREIGN KEY (personId)
    REFERENCES dbo.Person (personId)


    And then example of query:



    USE FirstDB;
    GO

    SET NOCOUNT ON;
    DECLARE @row int;

    UPDATE Person
    SET age = 40
    FROM dbo.Person as p join dbo.PersonAge as p1
    ON p.personId = p1.personId
    WHERE p.age = 60

    SET @row = @@ROWCOUNT
    SELECT @row


    I create an UPDATE query where none of rows will be affected.



    At the end @row consist 0 value.






    share|improve this answer





















    • 1





      Thanks @veljasije - which part of my UPDATE is a "simple assignment"? "Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'."

      – Mike
      Apr 16 '14 at 7:42











    • Thanks @veljasije - there was a rogue SET @Err = @@ERROR between my UPDATE statement and the setting of my variable. Amazing when someone else does code that works - it focussed your mind! Thanks again

      – Mike
      Apr 17 '14 at 8:13
















    3














    Here is an explanation:



    Statements that make a simple assignment always set the @@ROWCOUNT value to 1.


    More information you can find here:



    @@ROWCOUNT



    My example:



    CREATE DATABASE FirstDB
    GO

    USE FirstDB;
    GO

    CREATE TABLE Person (
    personId INT IDENTITY PRIMARY KEY,
    firstName varchar(20) ,
    lastName varchar(20) ,
    age int
    )

    INSERT INTO dbo.Person (firstName, lastName, age)
    VALUES ('Nick', 'Smith', 30),
    ('Jack', 'South', 25),
    ('Garry', 'Perth', 20)


    CREATE TABLE PersonAge (
    personAgeId INT IDENTITY PRIMARY KEY ,
    personId INT ,
    newAge varchar(10)
    )

    INSERT INTO dbo.PersonAge(personId, newAge)
    VALUES (1, 60),
    (2, 65),
    (3, 70)

    ALTER TABLE dbo.PersonAge
    ADD CONSTRAINT FK_PersonAgePerson FOREIGN KEY (personId)
    REFERENCES dbo.Person (personId)


    And then example of query:



    USE FirstDB;
    GO

    SET NOCOUNT ON;
    DECLARE @row int;

    UPDATE Person
    SET age = 40
    FROM dbo.Person as p join dbo.PersonAge as p1
    ON p.personId = p1.personId
    WHERE p.age = 60

    SET @row = @@ROWCOUNT
    SELECT @row


    I create an UPDATE query where none of rows will be affected.



    At the end @row consist 0 value.






    share|improve this answer





















    • 1





      Thanks @veljasije - which part of my UPDATE is a "simple assignment"? "Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'."

      – Mike
      Apr 16 '14 at 7:42











    • Thanks @veljasije - there was a rogue SET @Err = @@ERROR between my UPDATE statement and the setting of my variable. Amazing when someone else does code that works - it focussed your mind! Thanks again

      – Mike
      Apr 17 '14 at 8:13














    3












    3








    3







    Here is an explanation:



    Statements that make a simple assignment always set the @@ROWCOUNT value to 1.


    More information you can find here:



    @@ROWCOUNT



    My example:



    CREATE DATABASE FirstDB
    GO

    USE FirstDB;
    GO

    CREATE TABLE Person (
    personId INT IDENTITY PRIMARY KEY,
    firstName varchar(20) ,
    lastName varchar(20) ,
    age int
    )

    INSERT INTO dbo.Person (firstName, lastName, age)
    VALUES ('Nick', 'Smith', 30),
    ('Jack', 'South', 25),
    ('Garry', 'Perth', 20)


    CREATE TABLE PersonAge (
    personAgeId INT IDENTITY PRIMARY KEY ,
    personId INT ,
    newAge varchar(10)
    )

    INSERT INTO dbo.PersonAge(personId, newAge)
    VALUES (1, 60),
    (2, 65),
    (3, 70)

    ALTER TABLE dbo.PersonAge
    ADD CONSTRAINT FK_PersonAgePerson FOREIGN KEY (personId)
    REFERENCES dbo.Person (personId)


    And then example of query:



    USE FirstDB;
    GO

    SET NOCOUNT ON;
    DECLARE @row int;

    UPDATE Person
    SET age = 40
    FROM dbo.Person as p join dbo.PersonAge as p1
    ON p.personId = p1.personId
    WHERE p.age = 60

    SET @row = @@ROWCOUNT
    SELECT @row


    I create an UPDATE query where none of rows will be affected.



    At the end @row consist 0 value.






    share|improve this answer















    Here is an explanation:



    Statements that make a simple assignment always set the @@ROWCOUNT value to 1.


    More information you can find here:



    @@ROWCOUNT



    My example:



    CREATE DATABASE FirstDB
    GO

    USE FirstDB;
    GO

    CREATE TABLE Person (
    personId INT IDENTITY PRIMARY KEY,
    firstName varchar(20) ,
    lastName varchar(20) ,
    age int
    )

    INSERT INTO dbo.Person (firstName, lastName, age)
    VALUES ('Nick', 'Smith', 30),
    ('Jack', 'South', 25),
    ('Garry', 'Perth', 20)


    CREATE TABLE PersonAge (
    personAgeId INT IDENTITY PRIMARY KEY ,
    personId INT ,
    newAge varchar(10)
    )

    INSERT INTO dbo.PersonAge(personId, newAge)
    VALUES (1, 60),
    (2, 65),
    (3, 70)

    ALTER TABLE dbo.PersonAge
    ADD CONSTRAINT FK_PersonAgePerson FOREIGN KEY (personId)
    REFERENCES dbo.Person (personId)


    And then example of query:



    USE FirstDB;
    GO

    SET NOCOUNT ON;
    DECLARE @row int;

    UPDATE Person
    SET age = 40
    FROM dbo.Person as p join dbo.PersonAge as p1
    ON p.personId = p1.personId
    WHERE p.age = 60

    SET @row = @@ROWCOUNT
    SELECT @row


    I create an UPDATE query where none of rows will be affected.



    At the end @row consist 0 value.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 16 '14 at 20:12

























    answered Apr 16 '14 at 7:36









    veljasijeveljasije

    3,98273967




    3,98273967








    • 1





      Thanks @veljasije - which part of my UPDATE is a "simple assignment"? "Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'."

      – Mike
      Apr 16 '14 at 7:42











    • Thanks @veljasije - there was a rogue SET @Err = @@ERROR between my UPDATE statement and the setting of my variable. Amazing when someone else does code that works - it focussed your mind! Thanks again

      – Mike
      Apr 17 '14 at 8:13














    • 1





      Thanks @veljasije - which part of my UPDATE is a "simple assignment"? "Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'."

      – Mike
      Apr 16 '14 at 7:42











    • Thanks @veljasije - there was a rogue SET @Err = @@ERROR between my UPDATE statement and the setting of my variable. Amazing when someone else does code that works - it focussed your mind! Thanks again

      – Mike
      Apr 17 '14 at 8:13








    1




    1





    Thanks @veljasije - which part of my UPDATE is a "simple assignment"? "Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'."

    – Mike
    Apr 16 '14 at 7:42





    Thanks @veljasije - which part of my UPDATE is a "simple assignment"? "Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT 'Generic Text'."

    – Mike
    Apr 16 '14 at 7:42













    Thanks @veljasije - there was a rogue SET @Err = @@ERROR between my UPDATE statement and the setting of my variable. Amazing when someone else does code that works - it focussed your mind! Thanks again

    – Mike
    Apr 17 '14 at 8:13





    Thanks @veljasije - there was a rogue SET @Err = @@ERROR between my UPDATE statement and the setting of my variable. Amazing when someone else does code that works - it focussed your mind! Thanks again

    – Mike
    Apr 17 '14 at 8:13













    0














    Here is another example, using INSERT and DELETE--



    DECLARE @deletedRows INT = 0;
    SELECT @deletedRows = @@ROWCOUNT; --no previous DML statement
    SELECT @deletedRows; --@@ROWCOUNT = 1 for a simple assignment
    GO

    DROP TABLE IF EXISTS #Test;
    GO
    CREATE TABLE #Test (ID INT IDENTITY, CurrentDate DATETIME DEFAULT GETDATE());
    GO

    INSERT #Test DEFAULT VALUES; --INSERT a single row
    DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
    SELECT @deletedRows;
    GO

    DELETE FROM #Test WHERE 1=2; --no rows deleted
    DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
    SELECT @deletedRows;
    GO

    DELETE TOP (1) t FROM #Test t WHERE 1=1; --1 row deleted
    DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
    SELECT @deletedRows;
    GO

    DELETE TOP (1) t FROM #Test t WHERE 1=1; --no rows left to delete
    DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
    SELECT @deletedRows;
    GO





    share|improve this answer






























      0














      Here is another example, using INSERT and DELETE--



      DECLARE @deletedRows INT = 0;
      SELECT @deletedRows = @@ROWCOUNT; --no previous DML statement
      SELECT @deletedRows; --@@ROWCOUNT = 1 for a simple assignment
      GO

      DROP TABLE IF EXISTS #Test;
      GO
      CREATE TABLE #Test (ID INT IDENTITY, CurrentDate DATETIME DEFAULT GETDATE());
      GO

      INSERT #Test DEFAULT VALUES; --INSERT a single row
      DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
      SELECT @deletedRows;
      GO

      DELETE FROM #Test WHERE 1=2; --no rows deleted
      DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
      SELECT @deletedRows;
      GO

      DELETE TOP (1) t FROM #Test t WHERE 1=1; --1 row deleted
      DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
      SELECT @deletedRows;
      GO

      DELETE TOP (1) t FROM #Test t WHERE 1=1; --no rows left to delete
      DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
      SELECT @deletedRows;
      GO





      share|improve this answer




























        0












        0








        0







        Here is another example, using INSERT and DELETE--



        DECLARE @deletedRows INT = 0;
        SELECT @deletedRows = @@ROWCOUNT; --no previous DML statement
        SELECT @deletedRows; --@@ROWCOUNT = 1 for a simple assignment
        GO

        DROP TABLE IF EXISTS #Test;
        GO
        CREATE TABLE #Test (ID INT IDENTITY, CurrentDate DATETIME DEFAULT GETDATE());
        GO

        INSERT #Test DEFAULT VALUES; --INSERT a single row
        DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
        SELECT @deletedRows;
        GO

        DELETE FROM #Test WHERE 1=2; --no rows deleted
        DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
        SELECT @deletedRows;
        GO

        DELETE TOP (1) t FROM #Test t WHERE 1=1; --1 row deleted
        DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
        SELECT @deletedRows;
        GO

        DELETE TOP (1) t FROM #Test t WHERE 1=1; --no rows left to delete
        DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
        SELECT @deletedRows;
        GO





        share|improve this answer















        Here is another example, using INSERT and DELETE--



        DECLARE @deletedRows INT = 0;
        SELECT @deletedRows = @@ROWCOUNT; --no previous DML statement
        SELECT @deletedRows; --@@ROWCOUNT = 1 for a simple assignment
        GO

        DROP TABLE IF EXISTS #Test;
        GO
        CREATE TABLE #Test (ID INT IDENTITY, CurrentDate DATETIME DEFAULT GETDATE());
        GO

        INSERT #Test DEFAULT VALUES; --INSERT a single row
        DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
        SELECT @deletedRows;
        GO

        DELETE FROM #Test WHERE 1=2; --no rows deleted
        DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
        SELECT @deletedRows;
        GO

        DELETE TOP (1) t FROM #Test t WHERE 1=1; --1 row deleted
        DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 1
        SELECT @deletedRows;
        GO

        DELETE TOP (1) t FROM #Test t WHERE 1=1; --no rows left to delete
        DECLARE @deletedRows INT = @@ROWCOUNT; --@@ROWCOUNT = 0
        SELECT @deletedRows;
        GO






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 4:21

























        answered Nov 25 '18 at 4:13









        GraemeGraeme

        828912




        828912






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23102805%2frowcount-returning-1-when-no-update-made%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'