Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,
Im currently having an issue when executing with @employeeinfo at the last execute statement. Is this a declaration issue or string issue? how can i fix?

Im trying to pass through a stored procedure that generates a payroll for an employee.

@employeeinfo should be passing through a TVP but I'm getting "must declare scalar Variable @employeeinfo"

Thanks in advance


SQL
IF EXISTS(SELECT * FROM sys.Databases WHERE name='usp_createPayroll')
    BEGIN
        DROP PROCEDURE dbo.usp_createPayroll
        DROP TYPE dbo.EmployeeInfo
        DROP TYPE dbo.AllowanceInfo
    END
    
    CREATE TYPE EmployeeInfo AS TABLE 
    (
        employeeID INT
        ,TotalHours INT
        ,PRIMARY KEY (
            employeeID
            ,TotalHours
            )
    );
    GO
    CREATE TYPE AllowanceInfo AS TABLE 
    (
        employeeID INT, allowanceID INT, Amount DECIMAL(7, 2),PRIMARY KEY (
            employeeID, allowanceID, Amount
            )
    );
    GO
    
    CREATE PROCEDURE usp_createPayroll @StartDatePeriod DATE ,@EndDatePeriod DATE, @taxID INT, @numberofTotalHours EmployeeInfo READONLY, @Allowance AllowanceInfo READONLY
    AS
    BEGIN
    	DECLARE @stDate DATE,
    			@EdDate DATE,
    			@tx	INT,
    			@noHours INT,
    			@allow INT
    	SELECT p.employeeID,
    		  @taxID, @StartDatePeriod,
    		  @EndDatePeriod,
    		  p.TotalHours,
    		  pos.hourlyRate * p.TotalHours,
    		 (pos.hourlyRate * p.TotalHours) + h.AmountSold,
    		 ((pos.hourlyRate * p.TotalHours) + h.AmountSold) * t.taxRate, 
    		 ((pos.hourlyRate * p.TotalHours) + h.AmountSold) - (((pos.hourlyRate * p.TotalHours) + h.AmountSold) * t.taxRate)
    	FROM PaySlip p
    	INNER JOIN HumanResources h ON h.EmployeeID = p.EmployeeID
    	INNER JOIN Taxes t ON t.TaxID = p.TaxID
    	INNER JOIN Position pos ON pos.HourlyRate = p.EmployeeID
    	-- add position in somehow
    
    END
    
    
    DECLARE @employeeInfo EmployeeInfo;
    DECLARE @TotalHours INT;
    
    INSERT @employeeInfo
    SELECT e.employeeID, @TotalHours
    FROM HumanResources e
    WHERE e.employeeID = 2 AND @TotalHours = 30
    
    DECLARE @allowanceInfo AllowanceInfo;
    DECLARE @employeeAllowanceInfo EmployeeInfo;
    
    INSERT @allowanceInfo
    SELECT e.employeeID, pa.allowanceID, pa.Amount
    FROM HumanResources e, Allowance a, PayAllowance pa, EmployeeAllowance ea, @employeeAllowanceInfo emp
    WHERE e.employeeID = emp.employeeID AND pa.AllowanceID = a.AllowanceID AND a.AllowanceID = ea.AllowanceID AND pa.allowanceID = 1
    
    EXECUTE usp_createPayroll 
         @startDatePeriod = '01-01-2015 23:59:59'
        ,@endDatePeriod = '01-31-2015 23:59:59'
        ,@taxID = 2
        ,@numberofTotalHours = @employeeInfo
        ,@Allowance = @allowanceInfo
    GO
    DROP PROCEDURE usp_createPayroll


What I have tried:

Ive tried changing the select statement and declaring the variables when originally i had an insert statement
Posted
Updated 25-Apr-17 8:35am
v3

1 solution

Works fine for me unless I forget to include the code from DECLARE @employeeInfo EmployeeInfo;If I omit that DECLARE from my selection before hitting F5 then I get the same error as you.

I.e. you have to run all of the following:
SQL
DECLARE @employeeInfo EmployeeInfo;
  DECLARE @TotalHours INT;

  INSERT @employeeInfo
  SELECT e.employeeID, @TotalHours
  FROM HumanResources e
  WHERE e.employeeID = 2 AND @TotalHours = 30

  DECLARE @allowanceInfo AllowanceInfo;
  DECLARE @employeeAllowanceInfo EmployeeInfo;

  INSERT @allowanceInfo
  SELECT e.employeeID, pa.allowanceID, pa.Amount
  FROM HumanResources e, Allowance a, PayAllowance pa, EmployeeAllowance ea, @employeeAllowanceInfo emp
  WHERE e.employeeID = emp.employeeID AND pa.AllowanceID = a.AllowanceID AND a.AllowanceID = ea.AllowanceID AND pa.allowanceID = 1

  EXECUTE usp_createPayroll
       @startDatePeriod = '01-01-2015 23:59:59'
      ,@endDatePeriod = '01-31-2015 23:59:59'
      ,@taxID = 2
      ,@numberofTotalHours = @employeeInfo
      ,@Allowance = @allowanceInfo
  GO

I can't really see the point of creating a Stored Procedure just to drop it immediately after running it.

And I don't think
SQL
IF EXISTS(SELECT * FROM sys.Databases WHERE name='usp_createPayroll')
    BEGIN
        DROP PROCEDURE dbo.usp_createPayroll
        DROP TYPE dbo.EmployeeInfo
        DROP TYPE dbo.AllowanceInfo
    END
is doing what you think it is - it should probably be
SQL
IF EXISTS(SELECT * FROM sys.procedures WHERE name='usp_createPayroll')
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900