VBS 确定 SQL 表中的记录数 [英] VBS determine number of records in SQL table

查看:44
本文介绍了VBS 确定 SQL 表中的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定 SQL 表中的记录数,然后将该值保存在变量中?

How do I determine the number of records in a SQL table and then save that value in variable?

推荐答案

获取记录集大小/查询结果的三种方法:

Three methods to get the size of a recordset/result of a query:

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

使用文档 以了解有关这些策略的更多信息.

Use the Docs to learn more about these strategies.

这篇关于VBS 确定 SQL 表中的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆