Microsoft Knowledge Base Email Alertz

When you use the SQL Server OLE DB to modify a table whose name contains spaces, and you use a catalog to obtain table information from the database, and you then attempt to use the

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 294180 - Last Review: August 24, 2001 - Revision: 1.0

BUG: ADO: Append Cannot Be Used with OLE DB Provider For SQL Server if the Name of the Table Contains Spaces

This article was previously published under Q294180

On This Page

SYMPTOMS

When you use the SQL Server OLE DB to modify a table whose name contains spaces, and you use a catalog to obtain table information from the database, and you then attempt to use the Append method with ADO, you may receive the following error message:
Incorrect syntax near the keyword 'add'.

CAUSE

A table name that contains spaces must be enclosed in quotation marks ("") or brackets ([]). After it obtains catalog information on a table whose name contains spaces, ADO does not enclose the table name with quotation marks or brackets when Append calls are made.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. In Microsoft Visual Basic, create a Standard EXE project. Form1 is created by default.
  2. In Form1, create two command boxes.
  3. In Form1, paste the following code:
    Private Sub Command1_Click()
       On Error GoTo errorhandler
       Dim cat As New ADOX.Catalog
       Dim tbl As New ADOX.Table
       Dim Idx As New ADOX.Index
       Dim Cnn1 As New ADODB.Connection
       
       strTable = "[tbl wthspc]"  
     'Here we are setting the table name ourselves so brackets will be passed 
    'to the SQL server as the part of our query, so you will not get an error.
     
    
      'Change the server name, user ID, and password here.
       Cnn1.ConnectionString = "Provider=SQLOLEDB;Server=ServerName;UID=UserID;PWD=Password;database=DatabaseName;"
    
       Cnn1.Open
       
       Set cat.ActiveConnection = Cnn1
       tbl.Name = strTable
       tbl.Columns.Append "ChmpIdx1", adVarWChar, 50
       tbl.Columns.Append "ChmpIdx2", adVarWChar, 10
       cat.Tables.Append tbl
      
       Test cat, tbl
       Set tbl = Nothing
       Set cat = Nothing
       Cnn1.Close
       Set Cnn1 = Nothing
       
       Exit Sub
    errorhandler:
       MsgBox Err.Description, , Err.Source
    End Sub
    Private Sub Test(cat As ADOX.Catalog, tbl As ADOX.Table)
       On Error GoTo errorhandler1
       Dim Idx As New ADOX.Index
       Dim str As String
       
       Idx.Name = "index01"
       Idx.Unique = True
       Idx.PrimaryKey = True
       Idx.Columns.Append "ChmpIdx1"
       tbl.Indexes.Append Idx
       MsgBox "index created successfully"
       Set tbl = Nothing
       
       Exit Sub
    errorhandler1:
       MsgBox Err.Description, , Err.Source
       
    
    End Sub
    
    Private Sub Command2_Click()
       On Error GoTo errorhandler1
       Dim Cnn1 As New ADODB.Connection
       Dim cat As New ADOX.Catalog
       Dim tbl As ADOX.Table
       Dim Idx As New ADOX.Index
       Dim str As String
       
       strTable = "tbl wthspc"
      'Change the server name, user ID, and password here.
       Cnn1.ConnectionString = "Provider=SQLOLEDB;Server=ServerName;UID=UserID;PWD=Password;database=Pubs;"
       Cnn1.Open
     
      'Here we are calling a catalog function to get catalog information.
      
       Set cat.ActiveConnection = Cnn1
       Set tbl = cat.Tables(strTable) 
       Idx.Name = "index02"
       Idx.Unique = True
       Idx.PrimaryKey = True
       Idx.Columns.Append "ChmpIdx1"
       tbl.Indexes.Append Idx
       MsgBox "index created successfully"
       Set tbl = Nothing
       
       Set cat = Nothing
       Cnn1.Close
       Set Cnn1 = Nothing
       Exit Sub
    errorhandler1:
       MsgBox Err.Description, , Err.Source
    
       
    End Sub
    
    					
  4. Add a reference to the ADO Ext. 2.5 for DDL and Security object library in the project.
  5. Change the SQL Server alias, User ID, and password in the code.
  6. Run the project and click the first command box. This creates a table with spaces in the name (tbl wthspc) on the server, and it also creates the index01 index on the table because the table name is enclosed in brackets. If you trace this call in SQL Profiler, you see the table name as [tbl wthspc].
  7. Click the second command box. When you click this box, an attempt is made to append the index02 index on the table. In this step, you obtain the catalog information from the database, and because ADO does not enclose the table name in quotation marks or brackets, you receive an error message.
If you trace this call in SQL Profiler, you receive the following SQL statement:
alter table tbl wthspc add constraint index02 primary key  nonclustered(ChmpIdx1)
				
This statement shows that ADO does not add the required quotation marks or brackets around the tbl wthspc table name and that SQL Server is generating a syntax error.

APPLIES TO
  • Microsoft ActiveX Data Objects 2.5
  • Microsoft ActiveX Data Objects 2.6
  • Microsoft ActiveX Data Objects 2.7
  • Microsoft OLE DB Provider for SQL Server 7.0
  • Microsoft OLE DB Provider for SQL Server 2000 2000.80.194
Keywords: 
kbbug kbpending kbsqlprog KB294180
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate