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
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