Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Missing Temp Tables

By Andy Warren,

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

Total article views: 4148 | Views in the last 30 days: 0
 
Related Articles
FORUM
ARTICLE

Six Figures

This week Steve Jones looks at some data professional jobs that pay over six figures.

FORUM

Is a Recursive Query what I need?

Figuring out Principal payments

FORUM

query

query

FORUM

Query

Query

Tags
ado    
programming    
sql puzzles    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones