我编写了一个存储过程来将数据插入表中。当我执行到SQL Server 2012时,它在VBA抛出错误
时工作正常过程或函数需要未提供的参数@empid
我尝试使用连接字符串CommandType
等的不同组合来解决,但它无法解决。虽然代码看起来很好,但请帮我解决。
存储过程:
CREATE PROCEDURE [dbo].[spInsertDataIntoEmployee]
@empid INT,
@empname VARCHAR(20),
@empage INT,
@empsalary DECIMAL(8,2)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [dbo].[employee] (empid, empname, empage, empsalary)
VALUES (@empid, @empname, @empage, @empsalary)
END
VBA代码:
Private Sub Exec_StoredProcFromExcel_Click()
Dim con As ADODB.Connection
Dim res As ADODB.Recordset
Dim mobjCmd As ADODB.Command
Dim strConn As String
Dim par1 As Object
Dim par2 As Object
Dim par3 As Object
Dim par4 As Object
'Dim empname As Varchar
'Dim empage As Integer
'Dim empsalary As Single
Dim indRecordSetFields As Integer
'Dim strQuery As String
'con.Close
Set con = New ADODB.Connection
Set res = New ADODB.Recordset
Set mobjCmd = New ADODB.Command
strConn = "Driver={SQL Server Native Client 11.0};Server=localhost;Database=test;Trusted_Connection=yes;"
'strConn = "Provider=SQLOLEDB; Data Source=localhost; Initial Catalog=test; Integrated Security=SSPI;"
con.Open strConn, adOpenStatic, adLockOptimistic
Set par1 = mobjCmd.CreateParameter("@empid", adInteger, adParamInput)
Set par2 = mobjCmd.CreateParameter("@empname", adVarChar, adParamInput, 30)
Set par3 = mobjCmd.CreateParameter("@empage", adInteger, adParamInput)
Set par4 = mobjCmd.CreateParameter("@empsalary", adDouble, adParamInput)
With mobjCmd
.ActiveConnection = con
.CommandText = "Exec [dbo].[spInsertDataIntoEmployee]"
.CommandType = 1 'adCmdStoredProc=4 does not work while adCmdText=1 and adCmdUnknown=8 both work for me
.CommandTimeout = 45
.Parameters.Append par1
.Parameters("@empid").Value = 111
.Parameters.Append par2
.Parameters("@empname").Value = "majid rajih"
.Parameters.Append par3
.Parameters("@empage").Value = 34
.Parameters.Append par4
.Parameters("@empsalary").Value = 200000
'.Parameters.Append .CreateParameter("@empid", adInteger, adParamInput, 111)
'.Parameters.Append .CreateParameter("@empname", adVarChar, adParamInput, 20, "majid khan")
'.Parameters.Append .CreateParameter("@empage", adInteger, adParamInput, 30)
'.Parameters.Append .CreateParameter("@empsalary", adSingle, adParamInput, 250000)
'.Parameters.Append .CreateParameter("@empid", adInteger, adParamInput, , ThisWorkbook.Sheets("employee").Range("A2").Value)
'.Parameters.Append .CreateParameter("@empname", adVarChar, adParamInput, 20, ThisWorkbook.Sheets("employee").Range("B2").Value)
'.Parameters.Append .CreateParameter("@empage", adInteger, adParamInput, , ThisWorkbook.Sheets("employee").Range("C2").Value)
'.Parameters.Append .CreateParameter("@empsalary", adSingle, adParamInput, , ThisWorkbook.Sheets("employee").Range("D2").Value)
' repeat as many times as you have parameters
.Execute
End With
'With res
'res.CursorType = adOpenStatic
'res.LockType = adLockOptimistic
'res.Open mobjCmd 'This executed the stored proc
'End With
End Sub
答案 0 :(得分:4)
您正在文本模式下运行命令
您的完整查询为Exec [dbo].[spInsertDataIntoEmployee]
,不包含任何参数。
CommandType
应该是adCmdStoredProc
,CommandText
应该是程序名称,而不是exec
。
.CommandType = adCmdStoredProc
.CommandText = "[dbo].[spInsertDataIntoEmployee]"
如果您想保留adCmdText
,请列出查询中的所有参数:
.CommandType = adCmdText
.CommandText = "exec [dbo].[spInsertDataIntoEmployee] @empid, @empname, @empage, @empsalary"
答案 1 :(得分:0)
.CommandText =" spInsertDataIntoEmployee" 并尝试将所有内容发送为nvarchar。