Excel VBA中的存储过程会引发错误“无参数和参数”错误

时间:2022-12-08 09:32:02

I have a stored procedure written in SQL Server Management studio that I need to run from a tool built in VBA in Excel. When I run the code, it returns this error

我有一个用SQL Server Management studio编写的存储过程,我需要从Excel中的VBA内置工具运行。当我运行代码时,它返回此错误

Run time error - '2147217900 (80040e14): [Microsoft][ODBC SQL Server Drive][SQL Server]Procedure uspDailyUpdateHistoricalMSL has no parameters and arguments were supplied.

运行时错误 - '2147217900(80040e14):[Microsoft] [ODBC SQL Server驱动器] [SQL Server]过程uspDailyUpdateHistoricalMSL没有提供参数和参数。

Here is the stored procedure in SQL Server:

这是SQL Server中的存储过程:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[uspDailyUpdateHistoricalMSL] 
AS
    DECLARE @location AS VARCHAR
    DECLARE @employeegroup AS VARCHAR
    DECLARE @contractagency AS VARCHAR
    DECLARE @position AS VARCHAR
    DECLARE @ftpt AS VARCHAR
    DECLARE @userid AS VARCHAR
    DECLARE @bilingual as bit
    DECLARE @59email AS VARCHAR
    DECLARE @email AS VARCHAR
    DECLARE @59extension AS VARCHAR
    DECLARE @cca2 as bit
    DECLARE @giid AS VARCHAR
    DECLARE @guid AS VARCHAR
    DECLARE @cimid AS VARCHAR
    DECLARE @adminvisit AS VARCHAR
    DECLARE @firstname AS VARCHAR
    DECLARE @lastname AS VARCHAR
    DECLARE @agentname AS VARCHAR
    DECLARE @doh AS VARCHAR
    DECLARE @team AS VARCHAR
    DECLARE @title AS VARCHAR
    DECLARE @weekdayschedule AS VARCHAR
    DECLARE @weekendschedule AS VARCHAR
    DECLARE @manager AS VARCHAR
    DECLARE @supervisor AS VARCHAR


    -- Create New Hire
    INSERT INTO [PIA].[dbo].[HistoricalMasterStaffing] ([Year], NewHire, Location, EmployeeGroup, ContractAgency, Position, FTPT, 
                                                        WeekNumber, CreatedBy, CreatedDate, Bilingual, Five9email, Email, Five9extension, 
                                                        Cca2, Gicode, Staffguid, Staffcimid, Visit, Adminvisit, Firstname, Lastname, 
                                                        Agentname, DOH, Team, Title, Weekdayschedule, Weekendschedule, Manager, Supervisor)
    VALUES (DATEPART(YEAR, GETDATE()), 'TRUE', @location, @employeegroup, @contractagency, @position, @ftpt, 
            DATEPART(ISO_WEEK, GETDATE()), @userid, GETDATE(), @bilingual, @59email, @email, @59extension, 
            @cca2, @giid, @guid, @cimid, 'Admin Visit', @adminvisit, @firstname, @lastname, 
            @agentname, @doh, @team, @title, @weekdayschedule, @weekendschedule, @manager, @supervisor) 
GO 

Here is my VBA code:
  Sub updateemployee()
  Dim Cn As ADODB.Connection
  Dim Server_Name As String
  Dim Database_Name As String
  Dim CnCmd As ADODB.Command
  Server_Name = "SDL02-VM25"
  Database_Name = "PIA"
  Set CnCmd = New ADODB.Command
  Set Cn = New ADODB.Connection
  Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & _
         Database_Name & vbNullString
  With CnCmd
 .ActiveConnection = Cn
 .CommandText = "dbo.uspDailyUpdateHistoricalMSL"
 .CommandType = adCmdStoredProc
 .CommandTimeout = 0
 .Parameters.Append .CreateParameter("@modby", adVarChar, adParamInput, 200, getWinUser)
 .Parameters.Append .CreateParameter("@location", adVarChar, adParamInput, 200, EditStaff.Location.value)
 .Parameters.Append .CreateParameter("@employeegroup", adVarChar, adParamInput, 200, EditStaff.EmployGroup.value)
 .Parameters.Append .CreateParameter("@position", adVarChar, adParamInput, 200, EditStaff.Position.value)
 .Parameters.Append .CreateParameter("@contractagency", adVarChar, adParamInput, 200, EditStaff.Agency.value)
 .Parameters.Append .CreateParameter("@ftpt", adVarChar, adParamInput, 200, EditStaff.FTPT.value)
 .Parameters.Append .CreateParameter("@bilingual", adBoolean, adParamInput, , EditStaff.bilingual.value)
 .Parameters.Append .CreateParameter("@59email", adVarChar, adParamInput, 200, EditStaff.five9email.value)
 .Parameters.Append .CreateParameter("@email", adVarChar, adParamInput, 200, EditStaff.Email.value)
 .Parameters.Append .CreateParameter("@59extension", adVarChar, adParamInput, 200, EditStaff.Five9Extension.value)
 .Parameters.Append .CreateParameter("@cca2", adBoolean, adParamInput, , EditStaff.CCA2.value)
 .Parameters.Append .CreateParameter("@guid", adVarChar, adParamInput, 200, EditStaff.GUID.value)
 .Parameters.Append .CreateParameter("@cimid", adVarChar, adParamInput, 200, EditStaff.CIMID.value)
 .Parameters.Append .CreateParameter("@adminvisit", adVarChar, adParamInput, 200, EditStaff.adminvisit.value)
 .Parameters.Append .CreateParameter("@firstname", adVarChar, adParamInput, 200, EditStaff.firstname.value)
 .Parameters.Append .CreateParameter("@lastname", adVarChar, adParamInput, 200, EditStaff.lastname.value)
 .Parameters.Append .CreateParameter("@agentname", adVarChar, adParamInput, 200, "EditStaff.firstname.value & Space(2) &            EditStaff.lastname.value")
 .Parameters.Append .CreateParameter("@doh", adVarChar, adParamInput, 200, EditStaff.DOH.value)
 .Parameters.Append .CreateParameter("@team", adVarChar, adParamInput, 200, EditStaff.Team.value)
 .Parameters.Append .CreateParameter("@title", adVarChar, adParamInput, 200, EditStaff.Title.value)
 .Parameters.Append .CreateParameter("@weekdayschedule", adVarChar, adParamInput, 200, EditStaff.weekdayschedule)
 .Parameters.Append .CreateParameter("@weekendschedule", adVarChar, adParamInput, 200, EditStaff.weekendschedule.value)
 .Parameters.Append .CreateParameter("@manager", adVarChar, adParamInput, 200, EditStaff.Manager.value)
 .Parameters.Append .CreateParameter("@supervisor", adVarChar, adParamInput, 200, EditStaff.supervisor.value)
 .Execute

  End With
  Application.StatusBar = "Updating Employee Information..."

  End Sub

1 个解决方案

#1


3  

You didn't create the parameters in your stored procedure correctly.

您没有正确地在存储过程中创建参数。

Instead of this:

而不是这个:

CREATE PROCEDURE [dbo].[uspDailyUpdateHistoricalMSL] AS
DECLARE @location as varchar
DECLARE @employeegroup as varchar
...

You want this:

你要这个:

CREATE PROCEDURE [dbo].[uspDailyUpdateHistoricalMSL] 
 @location varchar,
 @employeegroup  varchar,
...
AS

Also you need to supply length values for your varchar parameters, though that isn't the reason for this particular error.

您还需要为varchar参数提供长度值,但这不是此特定错误的原因。

 @location varchar(31),  --or whatever the length of the location column is.

#1


3  

You didn't create the parameters in your stored procedure correctly.

您没有正确地在存储过程中创建参数。

Instead of this:

而不是这个:

CREATE PROCEDURE [dbo].[uspDailyUpdateHistoricalMSL] AS
DECLARE @location as varchar
DECLARE @employeegroup as varchar
...

You want this:

你要这个:

CREATE PROCEDURE [dbo].[uspDailyUpdateHistoricalMSL] 
 @location varchar,
 @employeegroup  varchar,
...
AS

Also you need to supply length values for your varchar parameters, though that isn't the reason for this particular error.

您还需要为varchar参数提供长度值,但这不是此特定错误的原因。

 @location varchar(31),  --or whatever the length of the location column is.