SQLServerCentral Article

Missing Temp Tables

,

Recently I was working on a small utility and ran into a problem you may find interesting. I had a 

fairly intense query that was going to be the basis for several other queries, so I decided to 

put the results of that query into a temp table. The next step would be to join to that table and 

perform some additional operations. 

Good plan, right? As I started to test, it seemed that the part that built the temp table worked 

fine, but follow up operations that used it failed. Why? Well, that's what I want you to tell me! 

I'm going to steal an idea from Sean Burke and put up five bucks of my own cash as the prize for 

figuring this out. Here are the rules:

  • Email your solution to me at awarren@cfl.rr.com. All entries must explain why the code below 

    fails AND how they figured it out.

  • I'll Paypal two bucks to the FIRST person with the correct

    solution.

  • Then I'll draw the names of three more people who submit the correct

    solution before midnight EST 

    on June 8, 2001 and Paypal them one dollar each!

  • I get to publish the names of the winning contestants along with their solutions in my follow 

    up article that will be posted on June 11, 2001.

Good luck!

Sub Test()

'5/27/01 Andy Warren

' Code that fails to execute as expected, can you figure out why?

'Requires a reference to ADO 2.6 and a local server with the pubs db

Dim cn As ADODB.Connection

Dim rs As ADODB.Recordset

Dim lCount As Long

On Error GoTo Handler

Set cn = New ADODB.Connection

cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial 

Catalog=pubs;Data Source=."

'get a list of ids we are concerned with, for this example

'just the ones pertaining to authors with a last name beginning

'with G

cn.Execute "select au_id into #TempIDList from authors where au_lname like 'g%'", lCount

Debug.Print "# of records in temp table: "; lCount

'now open a recordset based on the id's in the temp

'table - here we're just going to print the results,

'we probably be doing some kind of update normally

Set rs = New ADODB.Recordset

rs.Open "Select * from #TempIDList", cn

Do Until rs.EOF

    Debug.Print rs!Au_ID, rs!Au_Fname, rs!Au_Lname

    rs.MoveNext

Loop

rs.Close

Set rs = Nothing

Debug.Print "Done"

Exit Sub

Handler:

Debug.Print Err.Number, Err.Description

Resume Next

End Sub

See the answer to this puzzle at http://www.sqlservercentral.com/columnists/awarren/missingtemptablesfollowup.asp

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating