correlation name not found on SQL Anywhere

 ̄綄美尐妖づ 提交于 2020-01-11 13:14:08

问题


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

回答1:


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.




回答2:


Maybe you forgotten to refer the second table using the Alias 's' you created:

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 = s.SalaryId

   RETURN @totalAmountOfMoney;
END


来源:https://stackoverflow.com/questions/54199732/correlation-name-not-found-on-sql-anywhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!