如何确定SQL表中的记录数,然后将该值保存在变量中?
答案 0 :(得分:1)
使用ADO访问db:
SELECT count(0) FROM myTable
查询。未经测试的代码:
Dim conn, rs, recordsCount
recordsCount = -1
'initialize the connection
set conn = ...
'run the query and retrieve the results
set rs = conn.execute("SELECT count(0) as cnt FROM myTable")
if not rs.EOF then
recordsCount = cint(rs("cnt"))
end if
'cleanup
rs.close
conn.close
set rs = nothing
set conn = nothing
答案 1 :(得分:0)
获取记录集/查询结果大小的三种方法:
Option Explicit
' simple way to get a connection
Dim oDb : Set oDb = CreateObject("ADODB.Connection")
oDb.Open "dsn=NWIND"
' execute & !obtain result! of "SELECT COUNT()" query
WScript.Echo "Select Count(*):", oDb.Execute("SELECT COUNT(*) FROM Products").Fields(0).Value
' trying to use recordcount (fails, because non-static rs)
WScript.Echo "RecordCount (A):", oDb.Execute("SELECT * FROM Products").RecordCount
' use recordcount with static rs
Const adOpenStatic = 3
Dim oRs : Set oRS = CreateObject("ADODB.Recordset")
oRS.Open "SELECT * FROM Products", oDb, adOpenStatic
WScript.Echo "RecordCount (B):", oRs.RecordCount
' get rows from query and use UBound()
Dim aData : aData = oDb.Execute("SELECT * FROM Products").GetRows()
WScript.Echo "GetRows():", UBound(aData, 2) + 1
oDb.Close
输出:
cscript 04.vbs
Select Count(*): 77
RecordCount (A): -1
RecordCount (B): 77
GetRows(): 77
使用Docs了解有关这些策略的更多信息。