PRB: ADO Recordset Delete Method Deletes More Than a Single Record (290594)
The information in this article applies to:
- ActiveX Data Objects (ADO) 2.5
- ActiveX Data Objects (ADO) 2.6
- ActiveX Data Objects (ADO) 2.7
This article was previously published under Q290594 SYMPTOMS
Sometimes the Delete method of the ADO Recordset object may delete more than the row that you identify for deletion. This occurs only if the following items are true:
- The data source is SQL Server.
- The OLE DB provider is Microsoft OLE DB Provider for ODBC Drivers.
- The value of the CursorType property is anything other than adOpenDynamic.
- The columns that are specified in the resultset do not contain a unique field.
- The queried table does not contain a primary key.
If all of these conditions are true, and the Delete method is called on a record in the Recordset, all the records that match the values of the fields that are returned in the query are deleted instead of that single record being deleted.
CAUSE
When the OLE DB Provider for ODBC Drivers creates a Delete statement to send back to the backend datasource, it only includes the columns that are selected. Thus, if you select only a text field, this text field is used in the where condition for the Delete method when you call a Delete.
For example, suppose that you have a table with two fields: ID (an identity field) and Owner (a text field). Suppose that a Recordset is created from the following query:
SELECT Owner FROM Table WHERE ID = n
If you call the Delete method on the single record that is returned (because ID is an identity field), all records that match the value for Owner are deleted. An ODBC trace shows that the following DELETE statement is executed:
DELETE FROM Table WHERE Owner = ?
The OLE DB Provider for ODBC drivers is including all equally comparable columns in the rowset in the where clause. In the above example, because the original select only contains one text field, this is all that the OLE DB Provider for ODBC includes in the where clause.
RESOLUTION
There are two ways to work around this problem:
- Use the OLE DB Provider for SQL Server instead of the Microsoft OLE DB Provider for ODBC Driver.
- Select a unique field in addition to the text field.
For a code sample that demonstrates this, see the "More Information" section.
STATUS
This behavior is by design.
Modification Type: | Major | Last Reviewed: | 8/23/2001 |
---|
Keywords: | kbDatabase kbDSupport kbprb KB290594 |
---|
|