473,238 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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
4 49384
[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Corrine | last post by:
Hi, I am creating a global temporary table that is session-specific. I insert a BLOB into this table, and then select the BLOB from this table into a ResultSet. The ResultSet sees this BLOB...
1
by: Omavlana | last post by:
Hi, How can I create a temporary table say "Tblabc" with column fields ShmCoy char(2) ShmAcno char(10) ShmName1 varchar(60) ShmName2 varchar(60) and fill the table from the data extracted...
11
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the...
0
by: nedbollard | last post by:
Hi, Having checked out a declared (rather than created) temporary table successfully in SPUFI, I have hit a problem in running the same SQL in the cobol prog I am ameding to use it. I have:...
4
by: prasad | last post by:
I am getting sql error during binding a program which access a temporary table. The temporary table declaration and access methods are given below. EXEC SQL DECLARE GLOBAL TEMPORARY TABLE TEM88...
4
by: Coleen | last post by:
Hi All :-) Can anyone give me a URL where I can find a good example of code on how to create a temporary SQL table using VB.net? I've checked the Microsoft site at: ...
1
by: jenson | last post by:
<?php /* * Created on Apr 13, 2007 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ require_once 'init.php'; ...
0
by: santoshsri | last post by:
Hi, I want to create a temporary table in my ASP code to manipulate the data coming from DB2. I am using SQL = Declare Global Temporary table session.temp1 ( Id Decimal (12) , Name...
1
by: P3Eddie | last post by:
Hello All! Sorry if this is basic, but I'm having trouble finding a way to specify unique fields (like you would ADD CONSTRAINT in an ALTER TABLE statement) for a new table I'm creating with a...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.