2

I am copying my bulk data to SQL Server (table name: TmpTable) via C# code and then I want to update the table with following stored procedure:

ALTER PROCEDURE dbo.sp_Update_Locations
    (@lupdatedNoRow VARCHAR(10) OUT)
AS
    SET NOCOUNT ON
BEGIN

    DECLARE @mttblfaximages3_sql NVARCHAR(500) ='UPDATE testAdmin.dbo.mttblFaxImages2 set fRemoteStorageLocation = temp.RemoteStorageLocation, fRemoteImageName = temp.RemoteImageName  from testAdmin.dbo.mttblFaxImages2 T INNER JOIN #TmpTable Temp ON (T.fFaxId=Temp.PrimaryId  AND T.fFaxPageId=Temp.SecondaryId); DROP TABLE #TmpTable;SELECT @lupdatedNoRow = cast(@@rowcount as VARCHAR)' 

    EXEC sp_executesql @mttblfaximages3_sql
            select @lupdatedNoRow

END

I see update works fine but c# throws exception after that

Must declare the scalar variable "@lupdatedNoRow"

I want to return the number of rows updated.

How I should modify my stored procedure to return number of rows updated?

1 Answer 1

2

you need to define & pass the variable @lupdatedNoRow into the sp_executesql

EXEC sp_executesql @mttblfaximages3_sql, 
                   N'@lupdatedNoRow varchar(10) OUTPUT', 
                   @lupdatedNoRow OUTPUT
select @lupdatedNoRow
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Squirrel, once I update the SP as you said that error goes away but when I try to extract value for @lupdatedNoRow in c# , it returns 0. Though I see in table update has happened.
move the @@rowcount to immediately after the update and before the drop table. @@rowcount returns the affected rows from last operation. In your case it is the drop table
Ohh Shoot I should have noticed that ! Thanks Squirrel ! Time to buy SQL classes online.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.