SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Missing Temp Tables

By Andy Warren, 2001/06/01

Total article views: 4032 | Views in the last 30 days: 6

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

By Andy Warren, 2001/06/01

Total article views: 4032 | Views in the last 30 days: 6
Your response
 
 
Related tags

ADO     SQL Puzzles    
Programming     T-SQL    
 
Like this? Try these...
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com