我想在 ms access 2007 数据库查询中声明和设置变量。我想将 2 个数据库查询结果存储在 2 个变量中,因为它们在执行后是整数或字符串类型。我想知道如何声明和设置变量?
SQL server 等价物是这样的
declare @var1 varchar(50)
set @var1 = 'select * from table'
Run Code Online (Sandbox Code Playgroud)
Jet/ACE SQL不支持此语法。根据您的最终目标,您将需要使用 VBA(下面提供的示例)或子查询(如 @Thomas 的解决方案)来实现此类功能。
大致如下(改编自艾伦·布朗的网站):
Function DAORecordsetExample()
'Purpose: How to open a recordset and loop through the records.'
'Note: Requires a table named MyTable, with a field named MyField.'
Dim rs As DAO.Recordset
Dim strSql As String
strSql = "SELECT MyField FROM MyTable;"
Set rs = CurrentDb.OpenRecordset(strSql)
Do While Not rs.EOF
Debug.Print rs!MyField
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41608 次 |
| 最近记录: |