467,902 Members | 1,844 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,902 developers. It's quick & easy.

creating temporary table with SELECT INTO

Hi

Dose any body know why a temporary table gets deleted after querying it the
first time (using SELECT INTO)?
When I run the code bellow I'm getting an error message when open the temp
table for the second time.
Error Type:
Microsoft OLE DB Provider for SQL Server (0x80040E37)
Invalid object name '#testtable'.

----------------------------------------------------------------------------
---------------
cnn.Execute("SELECT category, product INTO #testtable FROM properties")
'---creating temporary TestTable and populate it with values from another
table

SET rst_testt = cnn.Execute("SELECT * from #testtable") '----- opening
the temporary TestTable
SET rst_testt2 = cnn.Execute("SELECT * from #testtable") '----- ERROR
opening the temporary TestTable for the second time (that where the error
occurred)

rst_testt2.Close '---- closing table connection
SET rst_testt2 = nothing

rst_testt.Close '----- closing table connection
SET rst_testt = nothing

cnn.Execute("DROP TABLE #testtable") '------ dropping the temporary
TestTable
'---------------------------------------------------------------------------
--------------
But when I create the temp table first and then INSERT INTO that table some
values then it is working fine.
'---------------------------------------------------------------------------
--------------
cnn.Execute("CREATE TABLE #testtable (category VARCHAR(3), product
VARCHAR(3))")
cnn.Execute("INSERT INTO #testtable VALUES('5','4')")

SET rst_testt = cnn.Execute("SELECT * from #testtable") '----- opening
the temporary TestTable
SET rst_testt2 = cnn.Execute("SELECT * from #testtable") '----- opening
the temporary TestTable for the second time

rst_testt2.Close '----- closing table connection
SET rst_testt2 = nothing

rst_testt.Close '----- closing table connection
SET rst_testt = nothing

cnn.Execute("DROP TABLE #testtable") '------ dropping the temporary
TestTable
'---------------------------------------------------------------------------
--------------

Does any body know why the first code (SELECT INTO) is not working where the
second code it working?

regards,
goznal
Jul 20 '05 #1
  • viewed: 48662
Share:
4 Replies
[posted and mailed, please reply in news]

gonzal (k2***@dodo.com.au) writes:
Dose any body know why a temporary table gets deleted after querying it
the first time (using SELECT INTO)?
When I run the code bellow I'm getting an error message when open the temp
table for the second time.
Error Type:
Microsoft OLE DB Provider for SQL Server (0x80040E37)
Invalid object name '#testtable'.

--------------------------------------------------------------------------
cnn.Execute("SELECT category, product INTO #testtable FROM properties")
'---creating temporary TestTable and populate it with values from another
table

SET rst_testt = cnn.Execute("SELECT * from #testtable")
'----- opening the temporary TestTable
SET rst_testt2 = cnn.Execute("SELECT * from #testtable")
'----- ERROR opening the temporary TestTable for the second time (that
where the error occurred)


What's happening is that ADO is opening a second connection behind your
back. This has really not anything to do with how you created the table.

The reason that ADO opens an extra connection, is because there are
rows waiting to be fetched on the first connection, so ADO cannot
submit a query on that connection. I cannot really say why it only happened
in one place, but it may be related to the type of cursor you picked,
and the fact that in the second example, there is only one row in
the table.

Use a static client-side cursor. I would expect this to resovle the
problem.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Thanks Erland
You were right.
ADO is opening an extra connection that’s why it is not working and the
second example is working because only one record was inserted. If I
change to second code to:
…
cnn.Execute("INSERT INTO #testtable VALUES('5','4')")
cnn.Execute("INSERT INTO #testtable VALUES('2','3')") ‘---- inserting
second row
…

Then it is not working, same as the first example.
As you have suggested in your previous email to use a static client-side
cursor, I have change to code to:

…
set rst_tre = Server.CreateObject("ADODB.RecordSet"
rst_tre.CursorLocation = 1 '-- adUseClient
rst_tre.CursorType = 3 '-- adOpenStatic
rst_tre.LockType = 2 ‘-- adLockOptimistic
sql_tre = "SELECT category, product INTO #testtable FROM products"
rst_tre.Open sql_tre,cnn
SET rst_tre = nothing
…

But it is still not working.
Any suggestion why?

I head a look at some logs from SQL:
…
SQL:BatchCompleted; SELECT category, product INTO #testtable FROM
products
SQL:BatchCompleted; SELECT * from #testtable
Audit Login -- network protocol:
SQL:BatchCompleted SELECT * from #testtable
Audit Logout
…

Any suggestions why ADO opens another connection after opening the
#testtable for the first time?

Another think I have noticed is that when I open the #testtable only
once it is working fine.

Thanks,
Gonzal
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
gonzal (no****@abc.abc) writes:
…
set rst_tre = Server.CreateObject("ADODB.RecordSet"
rst_tre.CursorLocation = 1 '-- adUseClient
rst_tre.CursorType = 3 '-- adOpenStatic
rst_tre.LockType = 2 ‘-- adLockOptimistic
sql_tre = "SELECT category, product INTO #testtable FROM products"
rst_tre.Open sql_tre,cnn
SET rst_tre = nothing
…

But it is still not working.
Any suggestion why?


I have to admit that I'm no ADO guru, so I cannot really say what is
happening. However I did a quick test, and this did not give me a
second connection:

Dim cnn As ADODB.Connection
Dim cnnErr As ADODB.Error
Set cnn = New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset

On Error GoTo errHandle

Dim oCommand As ADODB.Command

cnn.ConnectionString = "Provider=" & Trim$(Provider.Text) & ";" & _
"Data Source=" & Trim$(DataSource.Text) & ";" & _
"User Id=" & Trim$(User.Text) & ";" & _
"Password=" & Trim$(Password.Text) & ";" & _
"Initial Catalog=" & Trim$(Database.Text) & ";" & _
"Use Procedure for Prepare=0"
cnn.ConnectionTimeout = 5
cnn.CursorLocation = adUseClient
cnn.Open

Set oCommand = CreateObject("ADODB.Command")
Set oCommand.ActiveConnection = cnn

oCommand.CommandType = adCmdText
oCommand.CommandText = "select name into #temp from sysobjects"
oCommand.Execute

rs.Open "SELECT * FROM #temp", cnn
rs2.Open "SELECT * FROM #temp", cnn
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
Try to make the temporary table a global variable by using two #
characters like: ##testtable. This probably will solve your problem.

Greetings Sjaak
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Omavlana | last post: by
reply views Thread by nedbollard | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.