The variable name ‘@P10’ has already been declared. Variable names must be unique within a query batch or stored procedure.
When I execute this code below, above error comes on my SQL parameters @P10
at line Set oRS = .Execute
:
Sub SalveazaFactura(ByRef vRaspuns As Variant, ByRef vParametri As Variant)
On Error GoTo Err_Execute
Dim strMyQuery As String
strMyQuery = "SET NOCOUNT ON; " & vbCrLf & _
"DECLARE @P01 bigint = ?; DECLARE @P02 char(3) = ?; DECLARE @P03 nvarchar(50) = ?; DECLARE @P04 date = ?; DECLARE @P05 bigint = ?; " & vbCrLf & _
"DECLARE @P06 decimal(12, 2) = ?; DECLARE @P07 char(3) = ?; DECLARE @P08 nvarchar(255) = ?; DECLARE @P09 nvarchar(255) = ?; DECLARE @P10 nvarchar(50) = ?; " & vbCrLf
If vParametri(1) = 0 Then
strMyQuery = strMyQuery & _
" INSERT INTO [iScalaDB].[dbo].[CONTA_RegistruDocumenteIntrare_test] " & _
" ([Tip Document],[Nr Document],[Data Document],[Cod Emitent],[Valoare],[Moneda],[Stare 1],[Format 1],[Localizare 1],[Observatii 1],[Data Inregistrare 1],[Username 1]) " & _
" VALUES (@P02, @P03, @P04, @P05, @P06, @P07, 'C00', 'F00', 'L00', @P09, GETDATE(), @P10); " & vbCrLf & _
" SET @P01 = CAST(scope_identity() AS bigint); SET @P05 = CAST(@@ROWCOUNT AS bigint); " & vbCrLf & _
" UPDATE [iScalaDB].[dbo].[CONTA_RegistruDocumenteIntrare_test] SET [Nume fisier] = RIGHT('000000' + CONVERT(NVARCHAR, @P01), 6) + @P08 WHERE [ID] = @P01; " & vbCrLf & _
" SELECT @P05 AS 'Rows', @P01 AS IDOut; "
Else
strMyQuery = strMyQuery & _
" UPDATE [iScalaDB].[dbo].[CONTA_RegistruDocumenteIntrare_test] " & _
" SET [Tip Document] = @P02, [Nr Document] = @P03, [Data Document] = @P04, [Cod Emitent] = @P05, [Valoare] = @P06, [Moneda] = @P07, " & _
" [Nume fisier] = @P08, [Observatii 1] = @P09, [Data Inregistrare 2] = GETDATE(), [Username 2] = @P10 WHERE [ID] = @P01; " & vbCrLf & _
" SELECT @@ROWCOUNT AS 'Rows', @P01 AS ID; "
End If
Dim oDB As ADODB.Connection: Set oDB = New ADODB.Connection
Dim oCM As ADODB.Command: Set oCM = New ADODB.Command
Dim dbADOParameter As ADODB.Parameter
Dim oRS As ADODB.Recordset
Dim Err As ADODB.Error
oDB.Open gcConn & "; Collation=Latin1_General_CI_AS"
If Not oDB.State = adStateOpen Then
MessageBoxW Application.ActiveWindow.hWnd, StrPtr(Sheet3.Range("Mesaj004").Value), StrPtr("Eroare!"), 16
GoTo Err_Execute
End If
With oCM
.ActiveConnection = oDB
.CommandType = adCmdText
.Prepared = True
.NamedParameters = True
.CommandText = strMyQuery
.Parameters.Append .CreateParameter("@P01", adBigInt, adParamInput, 20, vParametri(1))
.Parameters.Append .CreateParameter("@P02", adVarWChar, adParamInput, 3, vParametri(2))
.Parameters.Append .CreateParameter("@P03", adVarWChar, adParamInput, 50, vParametri(3))
.Parameters.Append .CreateParameter("@P04", adDate, adParamInput, 7, vParametri(4))
.Parameters.Append .CreateParameter("@P05", adBigInt, adParamInput, 20, vParametri(5))
Set dbADOParameter = .CreateParameter("@P06", adDecimal, adParamInput, 14, vParametri(6))
dbADOParameter.Precision = 12: dbADOParameter.NumericScale = 2
.Parameters.Append dbADOParameter
.Parameters.Append .CreateParameter("@P07", adVarWChar, adParamInput, 3, vParametri(7))
If vParametri(8) = "" Then
.Parameters.Append .CreateParameter("@P08", adWChar, adParamInput, 4, Null)
Else
.Parameters.Append .CreateParameter("@P08", adVarWChar, adParamInput, 255, vParametri(8))
End If
If vParametri(9) = "" Then
.Parameters.Append .CreateParameter("@P09", adWChar, adParamInput, 4, Null)
Else
.Parameters.Append .CreateParameter("@P09", adVarWChar, adParamInput, 255, vParametri(9))
End If
.Parameters.Append .CreateParameter("@P10", adVarWChar, adParamInput, 50, vParametri(10))
Set oRS = .Execute
End With
If Not oRS.BOF And Not oRS.EOF Then
vRaspuns = oRS.GetRows()
Else
vRaspuns = Array(-1, 0)
GoTo Err_Execute
End If
oRS.Close
Set oRS = Nothing
oDB.Close
Set oDB = Nothing
Exit Sub
Err_Execute:
{...}
End Sub
- I exported strMyQuery and ran the query in SSMS without any errors
- I checked the information in the parameter collection of the
oCM
object and everything is fine: there are no two parameters with the name@P10
and each parameter has the correct value - I have checked the solutions given for other questions related to the same error, but they are NOT applicable in this case: the parameters are declared one by one without a loop.