rmk
asked on
Why is RecordsAffected=-1
I must be blind because I can't figure out why RecordsAffected=-1 in this trivial example.
Here's my SQL Server 2000 table definition:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblAmen itySpec]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tblAmenitySpec]
GO
CREATE TABLE [dbo].[tblAmenitySpec] (
[AmenityID] [int] NOT NULL ,
[AmenityTypeSpecID] [int] NOT NULL ,
[Flg] [bit] NOT NULL ,
[Qty] [decimal](18, 4) NULL ,
[AmenityAttrValueID] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblAmenitySpec] WITH NOCHECK ADD
CONSTRAINT [PK_tblAmenitySpec] PRIMARY KEY CLUSTERED
(
[AmenityID],
[AmenityTypeSpecID]
) ON [PRIMARY]
GO
Here's the update stored procedure:
CREATE PROCEDURE dbo.proctblAmenitySpec_Upd ateForAmen ityIDAmeni tyTypeSpec ID
(
@AmenityID INT,
@AmenityTypeSpecID INT,
@Flg BIT ,
@Qty DECIMAL(18,4) ,
@AmenityAttrValueID INT
)
AS
-------------------------- ---------- ---------- ---
-- local variables
-------------------------- ---------- ---------- ---
DECLARE
@Proc VARCHAR(50),
@RowCount INT,
@ErrNbr INT,
@ErrMsg VARCHAR(255)
-------------------------- ---------- ---------- ---
-- initialize
-------------------------- ---------- ---------- ---
SET @Proc = 'dbo.proctblAmenitySpec_Up dateForAme nityIDAmen ityTypeSpe cID'
SET NOCOUNT ON
-------------------------- ---------- ---------- ---
-- update
-------------------------- ---------- ---------- ---
UPDATE tblAmenitySpec
SET Flg = @Flg,
Qty = @Qty,
AmenityAttrValueID = @AmenityAttrValueID
WHERE AmenityID = @AmenityID AND
AmenityTypeSpecID = @AmenityTypeSpecID
-- handle errors
SELECT @ErrNbr = @@ERROR, @RowCount = @@ROWCOUNT
IF @ErrNbr <> 0 BEGIN
SET @ErrMsg = 'failed to update'
GOTO Proc_Err
END
-------------------------- ---------- ---------- ---
-- all is good
-------------------------- ---------- ---------- ---
RETURN 0
-------------------------- ---------- ---------- ---
-- error handler
-------------------------- ---------- ---------- ---
Proc_Err:
RAISERROR (@ErrMsg,16,1)
RETURN @ErrNbr
GO
Here' the script to load data into the table:
INSERT tblAmenitySpec VALUES (1,1,1,1,null)
Here's my snippet of Access XP VBA code which demonstrates the problem:
Public Sub RecAff _
( _
ByVal vstrDataSource As String, _
ByVal vstrInitialCatalog As String, _
ByVal vstrUserID As String, _
ByVal vstrPassword As String, _
ByVal vfUseProc As Boolean _
)
Dim lngAmenityID As Long
Dim lngAmenityTypeSpecID As Long
Dim strConnection As String
Dim fFlg As Boolean
Dim dblQty As Double
Dim lngRecAff As Long
Dim varValueID As Variant
Dim cmd As ADODB.Command
strConnection = "Provider=sqloledb;" & _
"Data Source=" & vstrDataSource & ";" & _
"Initial Catalog=" & vstrInitialCatalog & ";" & _
"User Id=" & vstrUserID & ";" & _
"Password=" & vstrPassword & ";"
lngAmenityID = 1
lngAmenityTypeSpecID = 1
fFlg = 1
dblQty = 999
varValueID = Null
Set cmd = New ADODB.Command
With cmd
If vfUseProc Then
.CommandText = "proctblAmenitySpec_Update ForAmenity IDAmenityT ypeSpecID"
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter(, adInteger, adParamReturnValue)
.Parameters.Append .CreateParameter("@Amenity ID", adInteger, adParamInput, , lngAmenityID)
.Parameters.Append .CreateParameter("@Amenity TypeSpecID ", adInteger, adParamInput, , lngAmenityTypeSpecID)
.Parameters.Append .CreateParameter("@Flg", adBoolean, adParamInput, , fFlg)
.Parameters.Append .CreateParameter("@Qty", adDouble, adParamInput, , dblQty)
.Parameters.Append .CreateParameter("@Amenity AttrValueI D", adInteger, adParamInput, , varValueID)
Else
.CommandText = "UPDATE tblAmenitySpec" _
& " SET Flg=" & Abs(fFlg) _
& ",Qty=" & dblQty _
& ",AmenityAttrValueID=" & IIf(IsNull(varValueID), "NULL", varValueID) _
& " WHERE AmenityID=" & lngAmenityID _
& " AND AmenityTypeSpecID=" & lngAmenityTypeSpecID
.CommandType = adCmdText
End If
.ActiveConnection = strConnection
.Execute lngRecAff
End With
Debug.Print lngRecAff
End Sub
The dynamic SQL update works and the recordsaffected value from the adodb command is 1 when I call the above vba code with:
call recaff ("(local)","databasename", "userid"," password", False)
The stored procedure update works but the recordsaffected value from the adodb command is -1 when I call the above vba code with:
call recaff ("(local)","databasename", "userid"," password", True)
Why do I get -1?
Here's my SQL Server 2000 table definition:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblAmen
drop table [dbo].[tblAmenitySpec]
GO
CREATE TABLE [dbo].[tblAmenitySpec] (
[AmenityID] [int] NOT NULL ,
[AmenityTypeSpecID] [int] NOT NULL ,
[Flg] [bit] NOT NULL ,
[Qty] [decimal](18, 4) NULL ,
[AmenityAttrValueID] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblAmenitySpec] WITH NOCHECK ADD
CONSTRAINT [PK_tblAmenitySpec] PRIMARY KEY CLUSTERED
(
[AmenityID],
[AmenityTypeSpecID]
) ON [PRIMARY]
GO
Here's the update stored procedure:
CREATE PROCEDURE dbo.proctblAmenitySpec_Upd
(
@AmenityID INT,
@AmenityTypeSpecID INT,
@Flg BIT ,
@Qty DECIMAL(18,4) ,
@AmenityAttrValueID INT
)
AS
--------------------------
-- local variables
--------------------------
DECLARE
@Proc VARCHAR(50),
@RowCount INT,
@ErrNbr INT,
@ErrMsg VARCHAR(255)
--------------------------
-- initialize
--------------------------
SET @Proc = 'dbo.proctblAmenitySpec_Up
SET NOCOUNT ON
--------------------------
-- update
--------------------------
UPDATE tblAmenitySpec
SET Flg = @Flg,
Qty = @Qty,
AmenityAttrValueID = @AmenityAttrValueID
WHERE AmenityID = @AmenityID AND
AmenityTypeSpecID = @AmenityTypeSpecID
-- handle errors
SELECT @ErrNbr = @@ERROR, @RowCount = @@ROWCOUNT
IF @ErrNbr <> 0 BEGIN
SET @ErrMsg = 'failed to update'
GOTO Proc_Err
END
--------------------------
-- all is good
--------------------------
RETURN 0
--------------------------
-- error handler
--------------------------
Proc_Err:
RAISERROR (@ErrMsg,16,1)
RETURN @ErrNbr
GO
Here' the script to load data into the table:
INSERT tblAmenitySpec VALUES (1,1,1,1,null)
Here's my snippet of Access XP VBA code which demonstrates the problem:
Public Sub RecAff _
( _
ByVal vstrDataSource As String, _
ByVal vstrInitialCatalog As String, _
ByVal vstrUserID As String, _
ByVal vstrPassword As String, _
ByVal vfUseProc As Boolean _
)
Dim lngAmenityID As Long
Dim lngAmenityTypeSpecID As Long
Dim strConnection As String
Dim fFlg As Boolean
Dim dblQty As Double
Dim lngRecAff As Long
Dim varValueID As Variant
Dim cmd As ADODB.Command
strConnection = "Provider=sqloledb;" & _
"Data Source=" & vstrDataSource & ";" & _
"Initial Catalog=" & vstrInitialCatalog & ";" & _
"User Id=" & vstrUserID & ";" & _
"Password=" & vstrPassword & ";"
lngAmenityID = 1
lngAmenityTypeSpecID = 1
fFlg = 1
dblQty = 999
varValueID = Null
Set cmd = New ADODB.Command
With cmd
If vfUseProc Then
.CommandText = "proctblAmenitySpec_Update
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter(, adInteger, adParamReturnValue)
.Parameters.Append .CreateParameter("@Amenity
.Parameters.Append .CreateParameter("@Amenity
.Parameters.Append .CreateParameter("@Flg", adBoolean, adParamInput, , fFlg)
.Parameters.Append .CreateParameter("@Qty", adDouble, adParamInput, , dblQty)
.Parameters.Append .CreateParameter("@Amenity
Else
.CommandText = "UPDATE tblAmenitySpec" _
& " SET Flg=" & Abs(fFlg) _
& ",Qty=" & dblQty _
& ",AmenityAttrValueID=" & IIf(IsNull(varValueID), "NULL", varValueID) _
& " WHERE AmenityID=" & lngAmenityID _
& " AND AmenityTypeSpecID=" & lngAmenityTypeSpecID
.CommandType = adCmdText
End If
.ActiveConnection = strConnection
.Execute lngRecAff
End With
Debug.Print lngRecAff
End Sub
The dynamic SQL update works and the recordsaffected value from the adodb command is 1 when I call the above vba code with:
call recaff ("(local)","databasename",
The stored procedure update works but the recordsaffected value from the adodb command is -1 when I call the above vba code with:
call recaff ("(local)","databasename",
Why do I get -1?
as for why:
"default cursor type returned from a connection.execute call is a forward-only, read-only recordset which does not support the .RecordCount (and .recordsaffected??) property." - TimCottee
"default cursor type returned from a connection.execute call is a forward-only, read-only recordset which does not support the .RecordCount (and .recordsaffected??) property." - TimCottee
In Microsoft Access true is = -1.
ASKER
This command executes an update stored procedure; therefore .cursorlocation is not applicable and a recordset is not applicable. Whether I pass true, -1, or 1 to the stored procedure is immaterial because mdac will do the conversion per the adBoolean argument.
The real answer is that copy and paste while working 20 hour days makes your eyes bac. Simply removing the SET NOCOUNT ON from the stored procedure solves the problem.
The real answer is that copy and paste while working 20 hour days makes your eyes bac. Simply removing the SET NOCOUNT ON from the stored procedure solves the problem.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
With cmd
.CursorLocation = adUseClient