作者:大眼刘 | 来源:互联网 | 2023-06-29 17:32
我正在将microsoft access 2016表单与SQL Server 2018和microsoft SQL Server Management Studio 18.4一起使用
我已经有:
UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;
可以。
当我如下定义AA
和BB
时:
Dim AA as integer
Dim AA as string
AA = 20
BB = 'PATRIK'
并运行为:
UPDATE Student SET NAME = BB WHERE Age = AA;
错误消息说:
无效的列名称“ BB”和“ AA”
我不需要直接在服务器上进行数据输入来写入AA
和BB
值(20
,PATRIK
)。
我尝试定义为@AA=20
,@bb='PATRIK'
,但是access 2016表单拒绝使用Prefix@
。
您必须串联变量:
SQL = "UPDATE Student SET [NAME] = '" & BB & "' WHERE Age = " & AA & ";"
CurrentDb.Execute SQL
或使用参数创建查询并设置参数值。
,
我会提出以下建议:
Dim AA As Integer
Dim BB As String
AA = 20
BB = "PATRIK"
With CurrentDb.CreateQueryDef("","update Student t set t.name = @name where t.age = @age")
.Parameters(0) = BB
.Parameters(1) = AA
.Execute
End With
以这种方式使用参数避免了担心格式化SQL语句中的数据类型的麻烦,并且避免了任何可能的SQL注入。