gpt4 book ai didi

sql - SQL 语句中的全局变量

转载 作者:行者123 更新时间:2023-12-04 23:43:38 24 4
gpt4 key购买 nike

我在 VBA 中有以下代码:

Dim strSQL As String
strSQL = "UPDATE Workstations SET MID = newvalue WHERE MID = tempvalue"
DoCmd.RunSQL strSQL

newvalue 和 tempvalue 都是全局变量,并且已经设置了值。语法明智,这有意义吗?还是我缺少引号?

最佳答案

试试这个:

如果MID数字:

Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = " & newvalue & " WHERE [MID] = " & tempvalue
DoCmd.RunSQL strSQL

如果 MIDstring(如果 newvalue/tempvalue 不包含单引号 ' ):

Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = '" & newvalue & "' WHERE [MID] = '" & tempvalue & "'"
DoCmd.RunSQL strSQL

如果 MIDstring(如果 newvalue/tempvalue 包含单引号 ' 喜欢 newvalue="Mike's car"):

Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = '" & Replace(newvalue, "'", "''") & "' WHERE [MID] = '" & Replace(tempvalue, "'", "''") & "'"
DoCmd.RunSQL strSQL

如果 MID日期:

Dim strSQL As String
strSQL = "UPDATE Workstations SET [MID] = #" & newvalue & "# WHERE [MID] = #" & tempvalue & "#"
DoCmd.RunSQL strSQL

感谢@HansUp 在评论中指出,

MID is the name of a function, so it would be safer to bracket or alias that field name in the SQL statement: SET [MID]

关于sql - SQL 语句中的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22563844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com