Detecting changes during insert/update

  • Sometimes you need to dynamically construct insert/update statement inside of your stored procedure. In this case you can not use @@rowcount variable to determine if anything happened to the updated table. Do you know any other way to check if something happened to the table after execution of dynamically constructed SQL string?

    Thanks,

    Alex

  • You can use sp_executesql to run the d-sql - set a return parameter from @@rowcount and return that.

    There is an example of using sp_executesql to return parameters from d-sql and d-sql SP calls at

    http://www.nigelrivett.com

    sp_executeSQL


    Cursors never.
    DTS - only when needed and never to control.

  • Hey!!!

    Where's our link!!!!!!!!!!!!!!!!!!1

    BTW, Nice answer to this question.

    Steve Jones

    steve@dkranch.net

  • Sorry - that site (amazing grapics!) is hosted by ntl and I've lost the ability to update it.

    I'll add you when I move it somewhere else. Plan to host it at home but paid work keeps getting in the way.

    I started it to hold the sp_executesql item as I can never remember the syntax and the help is not very clear.


    Cursors never.
    DTS - only when needed and never to control.

  • This should work for you

    DECLARE @Sql Nvarchar(500),

    @Var NVarchar(100)

    @rc int

    SET @Var = '@RC int output'

    SET @Sql = 'UPDATE authors set phone = phone where contract = 1 SELECT @li = @@ROWCOUNT'

    EXEC @Sql , @Var , @rc

    PRINT @rc

  • Sorry , This is the right answer , i've used the pubs database for the sample query

    DECLARE @Sql Nvarchar(500),

    @Var NVarchar(100),

    @rc int

    SET @Var = '@RC int output'

    SET @Sql = 'UPDATE authors set phone = phone where contract = 1 SELECT @li = @@ROWCOUNT'

    EXEC Sp_ExecuteSql @Sql , @Var , @rc OutPut

    PRINT @rc

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply