我正在尝试在从MS Access 2019中的VBA代码传递给SQL Server 2017的查询中使用GO语句。执行会导致运行时错误:
80040e14“ GO附近的语法不正确”。
与在SSMS中执行的查询完全一样。
我在做什么错?如果我删除GO行,它将起作用。
在访问模块中:
Public SQLDB As Object
Public ADOcom As Object
Public Const adCmdText as Long = 1
Sub CreateDB()
Dim sql As String, connectionstring As String
connectionstring = "Driver={SQL Server Native Client 11.0};" & _
"Server=MySQLServer;" & _
"Trusted_Connection=yes;" & _
"Provider=SQLNCLI11;" & _
"DataTypeCompatibility=80;" & _
"MARS Connection=True;"
Set SQLDB = CreateObject("ADODB.Connection")
Set ADOcom = CreateObject("ADODB.Command")
SQLDB.Open connectionstring
Set ADOcom.ActiveConnection = SQLDB
ADOcom.CommandType = adCmdText
sql = "USE Master;" & vbCrLf & _
"GO" & vbCrLf & _
"CREATE DATABASE MyTest"
ADOcom.CommandText = sql
ADOcom.Execute
End Sub
GO不是Transact-SQL语句,而是公认的命令由Sql Server Management Studio(即SSMS),SQLCMD和OSQL提供实用程序。
您不需要在commandText中使用GO语句