Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using temporary table in an ADO Recordset

Tags:

vb6

t-sql

Please see the code below:

rsTitle.Open "CREATE TABLE #TestTable (testtitle varchar(10)) INSERT INTO #TestTable values ('TestTitle') select * from #testtable", objCon.ActiveCon, adOpenStatic, adLockReadOnly

rsTitle is always closed after the line above is executed. Why is this? It should return one row.

like image 569
w0051977 Avatar asked May 07 '26 10:05

w0051977


1 Answers

Try using SET NOCOUNT ON. Put SET NOCOUNT ON in the SQL statement used with ADO, as shown below:

Dim adoCn As adoDb.Connection
Dim adoCm As adoDb.Command
Dim rsTitle As adoDb.Recordset

Set adoCn = New adoDb.Connection
...    
Set adoCm = New adoDb.Command
With adoCm
    Set .ActiveConnection = adoCn
    .CommandType = adCmdText
    .CommandText = "CREATE TABLE #TestTable (testtitle varchar(10)) " & _
                   "INSERT INTO #TestTable(testtitle) values ('TestTitle') " & _
                   "SELECT * FROM #TestTable go"
    .Execute 
End With

Set rsTitle = New adoDb.Recordset
With rsTitle
    Set .ActiveConnection = adoCn
    .Open "SET NOCOUNT ON"
End With
rsTitle.Open adoCm, , , ,
like image 190
chridam Avatar answered May 13 '26 10:05

chridam