Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

PRB: Recordset Based on SQL Server View Updates Base Tables


View products that this article applies to.

Symptoms

Trying to issue an Update or Resync command on a recordset based on a SQL Server view might generate the following error:
Run-time error '-2147217911 (80040e09)':
SELECT permission denied on object <table_name>, database <db_name>, owner <owner_name>

↑ Back to the top


Cause

When ADO requests metadata for each column in the recordset (column name, table name, and so forth), SQL Server returns the base table name or names rather than the name of the view itself. ADO then uses this information to generate queries to resynchronize and update data.

↑ Back to the top


Resolution

To resolve this problem, grant users the appropriate permissions to the underlying base tables that the view is based upon. Typically, users need at least SELECT permission on the tables.

The Requery method can be used in place of the Resync method if it is necessary. However, there is a performance penalty with this option.

If you are using SQL Server 2000, create the view with the View_MetaData clause of the Create View command. For this syntax and other information, see the SQL Server 2000 documentation.

↑ Back to the top


Status

This behavior is by design beginning with SQL Server version 7.0.

↑ Back to the top


More information

If a provider needs metadata information on a SQL statement, it queries the datasource to see what methods are available to it to request the metadata. If the data source responds with no native method of returning the metadata, the provider asks the Client Cursor Engine (CCE) to parse the SQL SELECT statement to figure out the metadata information long hand rather than relying on the more efficient method that was not available through the datasource.

This code might work with other data sources. This is because many data sources do not have such built-in optimized methods of returning metadata, like SQL Server, regarding a SQL statement.

Steps to Reproduce Behavior

  1. Start a new Visual Basic Standard EXE project. Form1 is added by default.
  2. From the Project menu, click References, and select the Microsoft ActiveX Data Objects.
  3. Paste the following code into the load event of Form1 and modify the ConnectionString property accordingly:

    Note You must change User ID=<username> and Password=<strong password> to the correct values before you run this code. Make sure that the User ID has the appropriate permissions to perform this operation on the database.
    Dim cn As ADODB.Connection
    Dim cn2 As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim strConn As String, strSQL As String
    
    ' Open Connect with Admin rights
    Set cn2 = New ADODB.Connection
    cn2.ConnectionString = "Provider=SQLOLEDB;database=pubs;User ID=<username>;Password=<strong password>;server=football"
    cn2.Open
    'Create a new login
    cn2.Execute "sp_addlogin testuser2"
    'Create a new view
    cn2.Execute "CREATE VIEW view_auth2 AS select * from authors"
    'Revoke all rights on the table the view is based on for the guest user account
    cn2.Execute "revoke all on authors from guest"
    'Grant all rights on the view for the guest account
    cn2.Execute "GRANT  SELECT ,  INSERT ,  DELETE ,  UPDATE  ON dbo.view_auth2  TO guest"
    
    'login with view the new login
    strConn = "Provider=SQLOLEDB;database=pubs;User ID=testuser2;Password=;server=football"
    Set cn = New ADODB.Connection
    cn.Open strConn
    
    'Open a recordset based on the view
    strSQL = "select * from view_auth2"
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    rs.Open strSQL, cn, adOpenStatic, adLockOptimistic, adCmdText
    
    'Change a value in the recordset and attempt to update the view
    rs!au_lname = rs!au_lname & "z"
    rs.Update 'Errors here because the action query is created against the base table
    
    'or to see potential problems with Resync, comment the previous 2 lines and  uncomment the following
    
    'rs.Resync adAffectCurrent 'errors here
    					
  4. Run the project.

↑ Back to the top


Keywords: kbdatabase, kbprb, kbserver, KB253673

↑ Back to the top

Article Info
Article ID : 253673
Revision : 5
Created on : 11/7/2003
Published on : 11/7/2003
Exists online : False
Views : 578