I'm using VBA adodb to write INSERT
statements in a SQL Server based on Excel row contents.
I'm using parameterized queries so my VBA code has the following form:
sqlStatement = "INSERT INTO dbo.mytable (" & Join(insertElement(0), ", ") & ") VALUES (" & Join(insertElement(2), ", ") & ")"
Set cm = New ADODB.Command
With cm
Debug.Print (sqlStatement)
.ActiveConnection = conn
.CommandText = sqlStatement
.CommandType = adCmdText
For Each e In insertElement(1)
Set Pm = .CreateParameter(, adVarChar, 3, 1024, e)
.Parameters.Append Pm
Next e
Set rs = .Execute
End With
where insertElement(0)
is an array of field names, ...(1) is an array of values, and ...(2) is an array of placeholder ?
's to support parameterization
When I run this code, I get an error
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server] Incorrect syntax near 'Output'
However, when I interrogate the sqlStatement
text, there is no 'Output' anywhere in the statement. The text is of the form:
INSERT INTO dbo.mytable ([my_field_1],[my_field_2],[somefieldshaveweirdcharslike+#], ...) VALUES (?, ?, ?, ...)
So, if I'm not providing the 'Output' command directly, and I can't directly see the statement because it's being processed on the server side, how can I diagnose the syntax?
3
in.CreateParameter(, adVarChar, 3, 1024, e)
indicated it is a value foradParamInputOutput
as compared toadParamInput
which has the value of 1 based on the documentation ofCreateParameter
. Because you are writing anINSERT
, it should be 1 (adParamInput
). – shahkalpesh