Re: Temporary table with SQL Server 7.0
- From: "Bob Barrows [MVP]" <reb01501@xxxxxxxxxxxxxxx>
- Date: Mon, 21 Aug 2006 11:17:26 -0400
Francois Houde wrote:
Hi everybody I'm using the code down below with VB6 and SQL Server
7.0. I want to use temporary table but it doesn't seem to work.
Any idea on what the problem could be ?
Public Sub main()
On Error GoTo main_err
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim intTotal As Integer
Dim lngRecords As Long
strUserID = "strUserId"
strPassword = "strPassword"
strDSN = "strDSN"
strBD = "strBD"
strconnect = "DSN=" & strDSN & ";UID=" & strUserID & ";PWD=" &
strPassword & ";DATABASE=" & strBD
Set cnn = New ADODB.Connection
cnn.Open strconnect
cnn.Execute "select * into #tmp1 from table1", lngRecords
'Here lngRecords has a value of 20
Set rst = New ADODB.Recordset
rst.Open "select * from #tmp1", cnn
intTotal = 0
'Here rst.Recordcount has a value of -1
With a default server-side forward-only cursor, Recordcount will always
contain -1.
If rst.RecordCount > 0 Then
rst.MoveFirst
You just opened the recordset - there is no need to execute a MoveFirst.
If the recordset contains records, it will already be pointing at the
first record.
And of course, this loop will never run (RecordCount is less than zero).
Do While Not rst.EOF
intTotal = intTotal + 1
rst.MoveNext
Loop
End If
'Here intTotal has a value of 0 but should be 20
Of course. The loop never ran ...
Debug.Print intTotal
cnn.Close
What is your goal here? Simply to count the records? If so:
Set rst = cnn.Execute("select count(*) from tmp1",,adCmdText)
Debug.Print rst(0)
rst.close:set rst=nothing
cnn.close: set cnn=nothing
If you need to get a count and do something with the data as well, you
have two options:
1. Switch to a more expensive client-side or server-side static cursor
which supports RecordCount, or
2. Cease your dependance on RecordCount and use a GetRows array to
process the data (http://www.aspfaq.com/show.asp?id=2193)
The ADO documentation can be found at msdn.com. Here is a link:
http://msdn.microsoft.com/library/en-us/ado270/htm/mdmscadoapireference.asp
Go there and look up the Open and Execute methods. You will see the
correct syntax for opening different cursor types and locations.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
.
- Prev by Date: Re: Maybe it's a bug
- Next by Date: ADO / XP / Win98
- Previous by thread: Re: Maybe it's a bug
- Next by thread: Re: Temporary table with SQL Server 7.0
- Index(es):
Relevant Pages
|