TVP中存储过程中员工的工资单

时间:2015-06-25 10:43:09

标签: sql sql-server stored-procedures sql-server-2012 table-valued-parameters

我正在尝试使用此存储过程生成员工工资单并将其存储在数据库中,但我遇到了一些问题。

我的执行指向错误:

  

必须将参数编号4和后续参数作为'@name =传递   值'。在使用表单'@name = value'之后,所有后续的   参数必须以'@name = value'的形式传递。

我输入的参数是:

  1. 开始日期
  2. 结束日期
  3. 员工工作时间信息 - 表值参数,包含员工ID和工资时间。
  4. 员工津贴信息 - 具有员工ID,津贴类型ID和津贴金额的表值参数。
  5. 代码:

    CREATE TYPE EmployeeInfo AS TABLE 
    (
        employeeID INT
        ,hoursWorked INT
        ,PRIMARY KEY (
            employeeID
            ,hoursWorked
            )
    );
    GO
    
    CREATE TYPE AllowanceInfo AS TABLE 
    (
        employeeID INT
        ,allowanceID INT
        ,allowanceAmount DECIMAL(7, 2)
        ,PRIMARY KEY (
            employeeID
            ,allowanceID
            ,allowanceAmount
            )
    );
    GO
    
    CREATE PROCEDURE usp_createPayroll @startDate DATE
        ,@endDate DATE
        ,@taxID INT
        ,@noHoursWorked EmployeeInfo READONLY
        ,@allowanceBonus AllowanceInfo READONLY
    AS
    BEGIN
        INSERT INTO Payslip
        SELECT @startDate
            ,@endDate
            ,n.hoursWorked
            ,p.hourlyRate
            ,p.hourlyRate * n.hoursWorked
            ,(p.hourlyRate * n.hoursWorked) + a.allowanceAmount
            ,((p.hourlyRate * n.hoursWorked) + a.allowanceAmount) * t.taxRate / 100
            ,@taxID
            ,a.allowanceID
            ,n.employeeID
        FROM @noHoursWorked n
            ,@allowanceBonus a
            ,Position p
            ,Employee e
            ,Tax t
        WHERE p.positionID = e.positionID
            AND e.employeeID = n.employeeID
            AND t.taxID = @taxID
    END
    
    DECLARE @employeeInfo EmployeeInfo;
    DECLARE @hoursWorked INT;
    
    INSERT @employeeInfo
    SELECT e.employeeID
        ,@hoursWorked
    FROM Employee e
    WHERE e.employeeID = 1
        AND @hoursWorked = 160
    
    DECLARE @allowanceInfo AllowanceInfo;
    DECLARE @empInfo EmployeeInfo
    
    INSERT @allowanceInfo
    SELECT e.employeeID
        ,a.allowanceID
        ,a.allowanceAmount
    FROM Employee e
        ,Allowance a
        ,@empInfo emp
    WHERE e.employeeID = emp.employeeID
        AND a.allowanceID = 1
    
    EXECUTE usp_createPayroll @startDate = '1/1/2015'
        ,@endDate = '1/31/2015'
        ,@taxID = 2
        ,@employeeInfo
        ,@allowanceBonus
    

1 个答案:

答案 0 :(得分:0)

正如错误所说,当您使用命名参数时,您需要为所有参数执行此操作,它看起来应该是:

EXECUTE usp_createPayroll 
     @startDate = '1/1/2015'
    ,@endDate = '1/31/2015'
    ,@taxID = 2
    ,@noHoursWorked = @employeeInfo
    ,@allowanceBonus = @allowanceInfo

此外,看起来您正在声明一个永远不会填充数据的变量DECLARE @empInfo EmployeeInfo,但是在声明它之后的连接中使用它。也许引用@empinfo的插入查询应该引用@employeeInfo