Search code examples
t-sqlsql-functionsqlanywhere

correlation name not found on SQL Anywhere


I have two tables called Employees and Salarygroup. PersonalID is the primary key of Employees and SalaryID is the primary key of Salarygroup. Inside the Employees table there is one more row called StartDat, which has date data type and tracks down the date when the employee started working at the company. Moreover, AmountInEuros is the salary that an employee gets every month and it has a numeric data type

I need to make a function, which count the total amount of money, that the employee has recieved so far from the company, but when I input the PersonalID I get an error saying *Correlation name 'Salarygroup' not found.

Could someone helo me understand why this is happening?

  ALTER FUNCTION "dba"."countTotalAmountOfMoney"(@PersonalID int)
   RETURNS int
   AS
   BEGIN
   DECLARE @totalAmountOfMoney int;
   SELECT  @totalAmountOfMoney = g.AmountInEuros * DATEDIFF(month, g.StartDat, 
        '2019-01-16') 
   FROM dba.Employees 
   Inner Join dba.Salarygroup s
   ON   dba.Employees.SalaryId = dba.Salarygroup.SalaryId

   RETURN @totalAmountOfMoney;
END

Solution

  • You have given the table an alias, so you need to use it. I would recommend using aliases for all the tables:

    DECLARE @totalAmountOfMoney int;
    
    SELECT @totalAmountOfMoney = s.AmountInEuros * DATEDIFF(month, e.StartDat, '2019-01-16') 
    FROM dba.Employees e INNER JOIN
         dba.Salarygroup s
         ON e.SalaryId = s.SalaryId
    WHERE e.personalID = @PersonalID;
    

    Note that the g alias is not defined in your query. StartDat comes from Employees, so I changed that to e. I am guessing that AmountInEuros comes from s.