SQL语句中的全局变量

前端之家收集整理的这篇文章主要介绍了SQL语句中的全局变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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

如果MID是字符串(如果newvalue / tempvalue不包含单引号’):

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

如果MID是字符串(如果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]

猜你在找的MsSQL相关文章