The proper use of MSSQL “go” in VBA

*爱你&永不变心* 提交于 2019-12-02 10:29:53

问题


I am about to run a batch what I programmed using MSSQL, like this:

create function xy ( 
.....
end
go

create function2 xy (
...
end
go

Then, I saved it in a file, what my macro reads it into a string and with ADO trying to run. Unfortunately, all the time I get the following error message: Incorrect syntax near 'go'. I have been reading about it, and only found the solution to split the batch, but I don't really want since I have it in one string variable. My connection string in vba is the following (first the public declared variables):

Public conn        As ADODB.Connection
Public rs          As ADODB.Recordset
Public cmd         As ADODB.Command
Public sConnString As String

On Local Error GoTo err
    sConnString = "Provider=SQLOLEDB.1;Data Source=localhost;" & _
                  "Initial Catalog=" & database & ";" & _
                  "Integrated Security=SSPI;"
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    Set cmd = New ADODB.Command
    conn.Open sConnString
    conn.CursorLocation = adUseClient
    cmd.ActiveConnection = conn

set rs = conn.Execute(mysqlstring)

I have also checked the file what I read in with vba and no problem with it, if I copy it then paste to microsoft sql, it works. It does not, if I use ADO.

Every help is much appreciated!

Marta


回答1:


GO is default batch terminator, it is not correct TSQL.

"Signals the end of a batch of Transact-SQL statements to the SQL Server utilities."

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

For your example make 2 calls to database or create these functions via SSMS.



来源:https://stackoverflow.com/questions/32212077/the-proper-use-of-mssql-go-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!