Error '80004005' only when SELECT DISTINCT with ASP Classic - vbscript

I am developing in ASP VBScript at work and need to run a SELECT DISTINCT query but I am having some troubles.
I have other queries in my code that work perfectly fine, that do not use SELECT DISTINCT.
Here is what I am using:
Dim sections()
c = 1
set conn=Server.CreateObject("ADODB.Connection")
set rs=Server.CreateObject("ADODB.Recordset")
conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=******;User ID=Admin;" & _
"DATA SOURCE=" & Server.MapPath("modules.mdb")
rs.open "SELECT DISTINCT section FROM modules WHERE area='First' ORDER BY lvl ASC",conn
ReDim sections(10)
do while not rs.EOF
sections(c) = rs("section")
c = c + 1
rs.MoveNext
loop
rs.Close
conn.Close
set rs = nothing
set conn = nothing
Which gives me this error:
error '80004005'
on the line of the SQL query

The only way to fix this is to use "GROUP BY" instead of "DISTINCT"
SELECT DISTINCT section FROM modules WHERE area='First' ORDER BY lvl ASC
SELECT section FROM modules WHERE area='First' GROUP BY section ORDER BY lvl ASC

Related

Right way for passing parameters for IN-subquery (classic ASP, ADODB.Command)

good afternoon
i'm going crazy with passing parameters to a very simple query with a subquery; i wrote hundreds of complicated queries, i'm afraid my fault is just in passing parameters to the subquery or everything else seems very normally manageable
making things simpler i prepare my query this way:
Set myConn = Server.CreateObject("ADODB.Connection")
myConn.Mode = 3 '3 = adModeReadWrite
myConn.Open MM_DEFDB_STRING
Set myCmd = Server.CreateObject("ADODB.Command")
myCmd.ActiveConnection = myConn
myCmd.CommandType = 1 '1 = adCmdText
myCmd.CommandText = strQuery 'this is my query with ? placeholders
myCmd.Prepared = false 'i won't repeat it
Set myRs = Server.CreateObject("ADODB.Recordset")
myRs.CursorType = 1 '0 = adOpenForwardOnly, 1 = adOpenKeyset, ...
myRs.LockType = 3 '3 = adLockOptimistic, ...
(here i prepare query and parameters)
myRs.Open myCmd
this should be correct, it is always the same for a lot of queries
1st case:
strQuery = SELECT * FROM TableA WHERE TableA.ID IN (SELECT myIDs FROM TableB WHERE TableB.NumericField=?) AND StringField=?
then i use this command cycling on two arrays, one for parameter values, another for types:
myCmd.Parameters.Append myCmd.CreateParameter("#Par" & i, VTypes(i), 1, 0, VValues(i))
here VValues is [8975, "011M1005D"] and VTypes is [3, 8]
result is 12 records, it is correct, i see those records on my database
2nd case (i only changed order of my two AND clauses):
strQuery = SELECT * FROM TableA WHERE StringField=? AND TableA.ID IN (SELECT myIDs FROM TableB WHERE TableB.NumericField=?)
VValues is ["011M1005D", 8975] and VTypes is [8, 3]
result is ZERO records, not an error but simply no records
WHY in your opinion ??? i would expect the same 12 records as before, the AND logic operator is simmetric!
3rd case (i use values on 2nd case instead of parameters):
strQuery = SELECT * FROM TableA WHERE StringField='011M1005D' AND TableA.ID IN (SELECT myIDs FROM TableB WHERE TableB.NumericField=8975)
result is correct: 12 records
curious, with plain values the order seems not important anymore
i also tried to debug the parameters collection, printing for each parameter it's value and it's type in order to check them just a line before the Open commmand: they seems correct and in the correct order in both cases
you are last chance or i have to rearrange a lot of code using some JOIN (but i really would understand my mistake before!). thank you for patience

Classic ASP with Oracle

I have a select statement which is running fine in sqlplus but when i m trying to run in Classic ASP code I am getting below error
ORA-01843: not a valid month
My Select Query is as below
SELECT YEAR_MONTH, LAST_DAY(TO_DATE(TO_DATE('201106','YYYYMM'),'MM/DD/YYYY')) AS MAX_END_DT FROM MONTH_DIM WHERE '04-28-2016' BETWEEN MONTH_START_DATE AND MONTH_END_DATE
ASP Code as Below
Dim rs
cmd.CommandType = adCmdText
cmd.CommandText = "SELECT YEAR_MONTH, LAST_DAY(TO_DATE(TO_DATE('" & MAXYEARMONTH & "','YYYYMM'),'MM/DD/YYYY')) AS MAX_END_DT FROM MONTH_DIM WHERE '" & END_DT & "' BETWEEN MONTH_START_DATE AND MONTH_END_DATE"
set rs = cmd.execute --here is problem
use TO_DATE('" & END_DT & "','MM/DD/YYYY')

Only 1 row in recordset but all rows in table get updated

The query retrieves a single record as is confirmed by the recordcount but every single row in the table gets updated
I am using vb6 and ms ado 2.8
The Firebird version is 2.5.4.26856 (x64).
Firebird ODBC driver 2.0.3.154
The computer is windows 7 home edition 64 bit
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cs As String
Dim dbPath As String
dbPath = "c:\Parkes\Parkes.fdb"
cs = "DRIVER={Firebird/Interbase(r) Driver}; DBNAME=localhost:" & dbPath & "; UID=SYSDBA; PWD=masterkey;"
cn.ConnectionString = cs
cn.Open
Dim sQuery As String
sQuery = "select memo from clients where clientID = 10021 "
rs.Open sQuery, cn, adOpenStatic, adLockOptimistic
If rs.BOF <> True Or rs.EOF <> True Then
'putting msgbox rs.recordcount here confirms only 1 record in recordset
rs.Movefirst
rs.Fields("memo") = "blah"
rs.Update
End If
Set rs = Nothing
Set cn = Nothing
If I alter the query slightly by also selecting a second column, the client surname then only rows with the same value in the surname column as that of of the row where the clientid is 10021 get edited.
sQuery = "select memo, surname from clients where clientID = 10021 "
I cannot understand how more than one row should be edited when the recordset contains only a single row
EDIT: Having read around the web a bit this is my understanding of what is happening.
It seems that the update method identifies which records to update based on the selected columns in the recordset.
So if you select fields a,b,c,d and are updating field a, it will only update records in the database whose values for a,b,c,d match those in the recordset.
The best way to ensure that you only update a single record is to include the primary key in the selected fields.
So if I had written my query as in the line below, only a single record would have been updated because the clientID column contains unique values.
sQuery = "select memo, clientID from clients where clientID = 10021 "
It makes sense thinking about it but the way I wrote the query originally seems to work fine, in my experience, with other databases or am I wrong?
I tested your code everything was fine and only update one row. I only want to suggest you a simple way to check whether record exist or not. that could be like this:
if rs.rows.count > 0 then
' no need to move recordset to first by default its on the first row
end if

VB6 Recordsets and SQL count

I am noticing that the number of records in a database table (select reference from datetable) suddenly increases when I start running the program below even though there are no new records added. Please note that I establish that the number of rows increases by running a query in SQL Studio Manager i.e. select reference from datetable. When the program stops; the number of records falls back to the original level. Here is the code. Why does this happen? There is no Primary Key in the table though Reference is unique.
rs.Open "select reference,value1,datefield from datetable where field1 = 'value1' " & _
"order by reference", objAuditCon.ActiveCon, adOpenStatic, adLockPessimistic
Do While Not rs.EOF
intReadCount = intReadCount + 1
DoEvents
If Not IsNull(rs("value1")) Then
testArray = Split(rs("value1"), ",")
rs2.Open "SELECT Date FROM TBL_TestTable WHERE Record_URN = '" & testArray(1) & "'", objSystemCon.ActiveCon, adOpenStatic, adLockReadOnly
If rs2.EOF Then
End If
If Not rs2.EOF Then
rs("DateField") = Format$(rs2("Date"), "dd mmm yy h:mm:ss")
rs.Update
intWriteCount = intWriteCount + 1
End If
rs2.Close
Else
End If
rs.MoveNext
Loop
rs.Close
It's a little confused, but if you are referring to the total given by "intReadCount" against how many rows you have in the table then it looks as though you are not correctly clearing this value. At the beginning of the procedure you would want to set "intReadCount" back to 0 before starting, you should then get constant results.
Updated: See comments below

VBA/ADODB Run-Time Error: 3704

The following VBA subroutine will run most queries just fine. (ie: SELECT * FROM DUAL)
Sub DisplayQuery(QueryString As String)
Dim rs As New ADODB.Recordset
Dim connStr As String
connStr = _
"Provider=MSDAORA.1;" _
& "User ID=abc;Password=123;" _
& "Data Source=xxx/xxx;"
out QueryString
rs.Open QueryString, connStr, adOpenStatic, adLockOptimistic
Range("DataTable").Clear
Cells(1, 1).CopyFromRecordset rs
End Sub
However, when I run the query below, the following error message immediately pops up: Run-time error '3704':Operation is not allowed when the object is closed.
with all_hours as
( select to_date('2009-11-03 05:00 PM','yyyy-mm-dd hh:mi PM') + numtodsinterval(level-1,'hour') hour
from dual
connect by level <= 4 /*hours*/
)
select h.hour
, count(case when h.hour = trunc(s.sampled_on,'hh24') then 1 end) sampled
, count(case when h.hour = trunc(s.received_on,'hh24') then 1 end) received
, count(case when h.hour = trunc(s.completed_on,'hh24') then 1 end) completed
, count(case when h.hour = trunc(s.authorized_on,'hh24') then 1 end) authorized
from all_hours h cross join sample s
group by h.hour
Why?
If I recall correctly, the ADO Command object has a default timeout (30 seconds, I think) which may be causing your problem: there should be a setting like
cn.ConnectionTimeout = (your value here)
which you could extend.
I just restructured my query (link), so that I could put it into a view. I then created a connection in Excel to view name.

Resources