dbTalk Databases Forums  

Stored Proc not updating multiple rows

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Stored Proc not updating multiple rows in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
EmbersFire
 
Posts: n/a

Default Stored Proc not updating multiple rows - 05-05-2005 , 05:37 AM






I'm using a stored proceedure which should update a number of rows in a
table depending on a key value supplied (in this case 'JobID'). But
what's happening is when I call the proc from within the program, only
one row gets updated.

So

When I call the proc from Query Analyser, all rows get updated.
When I call the proc from within the program, only one row gets updated

Any ideas as to why this is happening??

JobID Description Price Status
----------------------------------------------
73412 Documents:Item 3 .00 0
73412 Documents:Item 5 .00 0
73412 Documents:Item 2 .00 0
73412 Documents:Item 4 .00 0
73412 Documents:Item 1 .00 0

^^^^Only one record gets updated, so the table ends up being...

JobID Description Price Status
----------------------------------------------
73412 Documents:Item 3 .00 4
73412 Documents:Item 5 .00 0
73412 Documents:Item 2 .00 0
73412 Documents:Item 4 .00 0
73412 Documents:Item 1 .00 0


Public Sub UpdateAllItems() As Boolean
Dim objCnn As ADODB.Connection
Dim objCmd As ADODB.Command

Set objCnn = New ADODB.Connection
With objCnn
.ConnectionString = cnConn
.CursorLocation = adUseClient
.Open
End With
Set objCmd = New ADODB.Command
Set objCmd.ActiveConnection = objCnn
With objCmd
.CommandText = "sp_UpdateJobItem"
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter("@Status", adInteger,
adParamInput, 4, Me.Status)
.Parameters.Append .CreateParameter("@JobID", adInteger,
adParamInput, 4, Me.iJobID)
.Execute
End With
Set objCnn = Nothing
Set objCmd = Nothing
End Sub


-----------------------------------------------------------------




SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE dbo.sp_UpdateJobItem
@JobID As int
, @Status As int
AS

--
================================================== ===========================================
SET XACT_ABORT OFF -- Allow procedure to continue after
error
DECLARE @error integer -- Local variable to capture the
error OnHoldAction.
--
================================================== ===========================================

BEGIN TRANSACTION

UPDATE tbl_JobItems
SET Status = @status
WHERE JobID = @JobID

--
================================================== ===========================================
-- Check for errors
--
================================================== ===========================================
SELECT @error = @ERROR

If @error > 0
BEGIN
ROLLBACK TRANSACTION
END
Else
BEGIN
COMMIT TRANSACTION
END



GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


Reply With Quote
  #2  
Old   
David Portas
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 06:02 AM






Quote:
UPDATE tbl_JobItems
SET Status = @status
WHERE JobID = @JobID
As the WHERE clause is based only on JobID this UPDATE statement will
update every row with that JobID. How do you intend to determine which
row to update? What is the key of this table?

--
David Portas
SQL Server MVP
--



Reply With Quote
  #3  
Old   
EmbersFire
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 06:29 AM



I'm using the jobID field to determine which records to update. That's
the key. So for a given jobid of 73412 like in the example, all rows
with that id should be updated.


Reply With Quote
  #4  
Old   
David Portas
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 06:37 AM



Have you used SET ROWCOUNT 1 somewhere? To be sure, try putting SET
ROWCOUNT 0 in the proc just before the UPDATE.

--
David Portas
SQL Server MVP
--


Reply With Quote
  #5  
Old   
David Portas
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 06:41 AM



Also, you should NOT be using SP_ as a prefix for user procs. SP_ is
reserved for system procs. That's probably not the cause of your
present problem but there's a chance it *could* be: if you've
inadvertently created a system proc of the same name in master then you
might be executing some code other than what you see here. Believe it
or not that can happen.

--
David Portas
SQL Server MVP
--


Reply With Quote
  #6  
Old   
EmbersFire
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 06:42 AM



I set ROWCOUNT to 1. But no luck I'm afraid.


Reply With Quote
  #7  
Old   
David Portas
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 07:10 AM



Do you mean you tried SET ROWCOUNT 0? You need to have ROWCOUNT set to
0 to be sure all rows can be updated.

--
David Portas
SQL Server MVP
--


Reply With Quote
  #8  
Old   
EmbersFire
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 08:19 AM



Sorry, I mean SET ROWCOUNT 0 as you had suggested


Reply With Quote
  #9  
Old   
David Portas
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 08:35 AM



OK. If it's not something that you can reproduce in QA then set up a
trace in SQL Profiler to capture the actual statements executed by your
code. That should give you enough to reproduce and identify the
problem.

--
David Portas
SQL Server MVP
--


Reply With Quote
  #10  
Old   
EmbersFire
 
Posts: n/a

Default Re: Stored Proc not updating multiple rows - 05-05-2005 , 11:07 AM



I've used the profiler. It's feeding in the correct parameters and no
errors are occurring. Interestingly enough, I tried a version of the
code which executes an sql string and the same thing is happening with
that as well.

strSQL = "UPDATE tbl_jobitems SET status = " & Me.Status & " WHERE
jobid = " & Me.iJobID

objCnn.Execute(strSQL)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.