473,837 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("SE LECT category, product INTO #testtable FROM properties")
'---creating temporary TestTable and populate it with values from another
table

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

rst_testt2.Clos e '---- closing table connection
SET rst_testt2 = nothing

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

cnn.Execute("DR OP 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("CR EATE TABLE #testtable (category VARCHAR(3), product
VARCHAR(3))")
cnn.Execute("IN SERT INTO #testtable VALUES('5','4') ")

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

rst_testt2.Clos e '----- closing table connection
SET rst_testt2 = nothing

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

cnn.Execute("DR OP 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 49451
[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("SE LECT category, product INTO #testtable FROM properties")
'---creating temporary TestTable and populate it with values from another
table

SET rst_testt = cnn.Execute("SE LECT * from #testtable")
'----- opening the temporary TestTable
SET rst_testt2 = cnn.Execute("SE LECT * 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("IN SERT INTO #testtable VALUES('5','4') ")
cnn.Execute("IN SERT 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.CreateOb ject("ADODB.Rec ordSet"
rst_tre.CursorL ocation = 1 '-- adUseClient
rst_tre.CursorT ype = 3 '-- adOpenStatic
rst_tre.LockTyp e = 2 ‘-- adLockOptimisti c
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:BatchComple ted; SELECT category, product INTO #testtable FROM
products
SQL:BatchComple ted; SELECT * from #testtable
Audit Login -- network protocol:
SQL:BatchComple ted 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.CreateOb ject("ADODB.Rec ordSet"
rst_tre.CursorL ocation = 1 '-- adUseClient
rst_tre.CursorT ype = 3 '-- adOpenStatic
rst_tre.LockTyp e = 2 ‘-- adLockOptimisti c
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.Connectio n
Dim cnnErr As ADODB.Error
Set cnn = New ADODB.Connectio n
Dim rs As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset

On Error GoTo errHandle

Dim oCommand As ADODB.Command

cnn.ConnectionS tring = "Provider=" & Trim$(Provider. Text) & ";" & _
"Data Source=" & Trim$(DataSourc e.Text) & ";" & _
"User Id=" & Trim$(User.Text ) & ";" & _
"Password=" & Trim$(Password. Text) & ";" & _
"Initial Catalog=" & Trim$(Database. Text) & ";" & _
"Use Procedure for Prepare=0"
cnn.ConnectionT imeout = 5
cnn.CursorLocat ion = adUseClient
cnn.Open

Set oCommand = CreateObject("A DODB.Command")
Set oCommand.Active Connection = cnn

oCommand.Comman dType = adCmdText
oCommand.Comman dText = "select name into #temp from sysobjects"
oCommand.Execut e

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
13749
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 object, and I am able to get the binary input stream from this blob. However, when I invoke InputStream.read(byte) on this input stream, I get the following exception: java.io.IOException: ORA-01410: invalid ROWID
1
3452
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 from the statement...
11
16302
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 equivalent to the SQL 'with' using TSQL? If there is not one, what is the TSQL solution to creating a temporary table that is associated with an SQL statement? Examples would be appreciated. Thank you!!
0
2815
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: existing SQL declare temporary table insert in temporary table
4
12052
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 LIKE SYSIBM.SYSDUMMY1 END-EXEC. EXEC SQL INSERT INTO TEM88 SELECT * FROM SYSIBM.SYSDUMMY1 END-EXEC.
4
24216
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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_04_8jtx.asp which is okay at explaining the process but gives me absolutely no code examples. Unfortunately, I am one of those programmers that can read and
1
3330
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'; $con=mysql_connect("localhost","root",""); $dbc=mysql_select_db(test,$con)or die("Database select error:" . mysql_error($con));
0
1301
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 Varchar(400) ) on Commint Preserve Rows ; Commit When I am executing it ..
1
2901
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 SELECT / INTO query. The table is a list containing, among other fields, names of children (fname and lname), their birthdays and ship addresses. I'd like to pull all the fields from the master table into a new table, retaining only one of any...
0
9846
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9685
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10886
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10580
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7817
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5674
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.