如何return 更新行的详细信息?
How return details of updated row?
我在 MS Access 2016 中有数据库,想 运行 通过 Excel VBA 更新查询 - 这可行。在此之后我想 return 更新行的详细信息(来自两列的值 - ID,Col1)。现在我只有受影响的行数。如何实现?
我的查询:
UPDATE
(SELECT TOP 1 ID, Col1, Update_time, Update_user
FROM Table1
WHERE Update_user Is Null
ORDER BY Col2 DESC , ID) AS U_ROW
SET U_ROW.Update_time = Now(), U_ROW.Update_user = [username];
在 Excel VBA 我 运行 通过 ADODB.Command:
With baseRecordsetCommand
.ActiveConnection = objectConnection
.CommandType = adCmdStoredProc
.CommandText = "qryTest"
.NamedParameters = True
.Parameters.Append .CreateParameter("@username", adVarChar, adParamInput, 255, LCase(Environ("Username")))
.Execute recordsAffected
End With
对此不确定,但如果您有唯一 ID,则可以执行类似的操作。这是在 Access 中完成的,因此需要设置连接,因为我相信这是可以的。
Function sqlret() As String
Dim c As ADODB.Connection
Dim s As String
Dim r As ADODB.Recordset
Dim l As Long
Set c = CurrentProject.Connection
c.Execute "insert into tblprojects(projecttitle) values ('code1')"
Set r = New ADODB.Recordset
r.Open "select @@identity", c, 1
l = r.Fields(0).Value
r.Close
r.Open "select * from tblprojects where [projectid]=" & l, c, 1
sqlret = r.GetString
End Function
通常这些类型的操作会进入存储过程,但在 Ms Acces/Excel 中,您必须使用 VBA。最简单的方法是,在更新之前检索您要更新的 ID。
所以这是第一个:将它保存在一个变量中
SELECT TOP 1 ID
FROM Table1
WHERE Update_user Is Null
ORDER BY Col2 DESC , ID
然后
update Table1
SET Table1.Update_time = Now(), Table1.Update_user = @username where Id = @idFromFirstQuery;
其次是
select * from Table1 where Id = @idFromFirstQuery;
通过这种方式,您可以知道您正在更新的 ID、行。
或:
您也可以将 now()
转换为参数并提供时间戳作为参数。
喜欢:
update (table selection )
set update_time = '2020-19-03 10:0=10:10', update_user = 'User1';
更新后您可以执行以下操作:
Select *
from Table1
where update_user = 'User1' and update_time = '2020-19-03 10:0=10:10';
注意!! 您高度假设您提供的时间戳 + 用户名是唯一的。我个人不会使用这种方法。
我在 MS Access 2016 中有数据库,想 运行 通过 Excel VBA 更新查询 - 这可行。在此之后我想 return 更新行的详细信息(来自两列的值 - ID,Col1)。现在我只有受影响的行数。如何实现?
我的查询:
UPDATE
(SELECT TOP 1 ID, Col1, Update_time, Update_user
FROM Table1
WHERE Update_user Is Null
ORDER BY Col2 DESC , ID) AS U_ROW
SET U_ROW.Update_time = Now(), U_ROW.Update_user = [username];
在 Excel VBA 我 运行 通过 ADODB.Command:
With baseRecordsetCommand
.ActiveConnection = objectConnection
.CommandType = adCmdStoredProc
.CommandText = "qryTest"
.NamedParameters = True
.Parameters.Append .CreateParameter("@username", adVarChar, adParamInput, 255, LCase(Environ("Username")))
.Execute recordsAffected
End With
对此不确定,但如果您有唯一 ID,则可以执行类似的操作。这是在 Access 中完成的,因此需要设置连接,因为我相信这是可以的。
Function sqlret() As String
Dim c As ADODB.Connection
Dim s As String
Dim r As ADODB.Recordset
Dim l As Long
Set c = CurrentProject.Connection
c.Execute "insert into tblprojects(projecttitle) values ('code1')"
Set r = New ADODB.Recordset
r.Open "select @@identity", c, 1
l = r.Fields(0).Value
r.Close
r.Open "select * from tblprojects where [projectid]=" & l, c, 1
sqlret = r.GetString
End Function
通常这些类型的操作会进入存储过程,但在 Ms Acces/Excel 中,您必须使用 VBA。最简单的方法是,在更新之前检索您要更新的 ID。
所以这是第一个:将它保存在一个变量中
SELECT TOP 1 ID
FROM Table1
WHERE Update_user Is Null
ORDER BY Col2 DESC , ID
然后
update Table1
SET Table1.Update_time = Now(), Table1.Update_user = @username where Id = @idFromFirstQuery;
其次是
select * from Table1 where Id = @idFromFirstQuery;
通过这种方式,您可以知道您正在更新的 ID、行。
或:
您也可以将 now()
转换为参数并提供时间戳作为参数。
喜欢:
update (table selection )
set update_time = '2020-19-03 10:0=10:10', update_user = 'User1';
更新后您可以执行以下操作:
Select *
from Table1
where update_user = 'User1' and update_time = '2020-19-03 10:0=10:10';
注意!! 您高度假设您提供的时间戳 + 用户名是唯一的。我个人不会使用这种方法。