L
Lucvdv
I'm having a little problem using a temporary table (MSDE as database
engine).
Dim cn As New SqlConnection("Data Source=" & SQLServerName _
& ";Integrated Security=SSPI;Initial Catalog=TestDB;Pooling=No")
Dim cmd As New SqlCommand("", cn)
Dim tx As SqlTransaction
cn.Open()
tx = cn.BeginTransaction
cmd.Transaction = tx
cmd.CommandText = "SELECT * INTO #TEMP FROM [SomeTable] WHERE (...)"
cmd.ExecuteNonQuery()
cmd.CommandText = "SELECT COUNT(*) FROM #TEMP"
nEntries = cmd.ExecuteScalar
This fails with "Invalid object name '#TEMP'" in the 2nd query.
It succeeds when I replace all occurances of #TEMP by ##TEMP.
I tried the same in VB6 using ADO 2.8, and there it works with a single #.
#TEMP is used in a single connection, even a single transaction.
Shouldn't it exist as long as the connection exists?
Adding or omitting the "Pooling=No" parameter in the connection string
doesn't make any difference.
If I make it a global temporary table (##), how long will it exist?
Besides, it would require me to generate a unique table name to avoid
collissions if the application is running on two machines at the same time.
I'm using a temporary table for two reasons, of which I think the second
leaves no alternatives:
- several subsequent queries have to be executed that would otherwise all
contain a rather complex WHERE clause (selecting about half of the
records of the original table depending on their PK occurring in two
other tables, with secondary WHERE clauses on each of those).
I expect better performance by first copying the relevant records to a
temporary table.
- I want to create a snapshot of those records, to mask off alterations in
the real table while this is running.
engine).
Dim cn As New SqlConnection("Data Source=" & SQLServerName _
& ";Integrated Security=SSPI;Initial Catalog=TestDB;Pooling=No")
Dim cmd As New SqlCommand("", cn)
Dim tx As SqlTransaction
cn.Open()
tx = cn.BeginTransaction
cmd.Transaction = tx
cmd.CommandText = "SELECT * INTO #TEMP FROM [SomeTable] WHERE (...)"
cmd.ExecuteNonQuery()
cmd.CommandText = "SELECT COUNT(*) FROM #TEMP"
nEntries = cmd.ExecuteScalar
This fails with "Invalid object name '#TEMP'" in the 2nd query.
It succeeds when I replace all occurances of #TEMP by ##TEMP.
I tried the same in VB6 using ADO 2.8, and there it works with a single #.
#TEMP is used in a single connection, even a single transaction.
Shouldn't it exist as long as the connection exists?
Adding or omitting the "Pooling=No" parameter in the connection string
doesn't make any difference.
If I make it a global temporary table (##), how long will it exist?
Besides, it would require me to generate a unique table name to avoid
collissions if the application is running on two machines at the same time.
I'm using a temporary table for two reasons, of which I think the second
leaves no alternatives:
- several subsequent queries have to be executed that would otherwise all
contain a rather complex WHERE clause (selecting about half of the
records of the original table depending on their PK occurring in two
other tables, with secondary WHERE clauses on each of those).
I expect better performance by first copying the relevant records to a
temporary table.
- I want to create a snapshot of those records, to mask off alterations in
the real table while this is running.