调用本机函数'ISNULL'时参数计数不正确

时间:2014-01-27 12:30:12

标签: mysql sql-server-2008

我有一个查询,我试图从MS SQL Server 2008转换为MySQL。它运行良好的MSSQL, 我收到了错误 “Incorrect parameter count in the call to native function 'ISNULL'”。

我该如何解决这个问题?

SELECT DISTINCT
    dbo.`@EIM_PROCESS_DATA`.U_Tax_year,
    dbo.`@EIM_PROCESS_DATA`.U_Employee_ID,
    CASE
        WHEN dbo.`@EIM_PROCESS_DATA`.U_PD_code = 'SYS033' THEN SUM(dbo.`@EIM_PROCESS_DATA`.U_Amount)
    END AS PAYE,
    CASE
        WHEN dbo.`@EIM_PROCESS_DATA`.U_PD_code = 'SYS014' THEN SUM(dbo.`@EIM_PROCESS_DATA`.U_Amount)
    END AS TOTALTAXABLE,
    dbo.OADM.CompnyName,
    dbo.OADM.CompnyAddr,
    dbo.OADM.TaxIdNum,
    dbo.OHEM.lastName + ', ' + ISNULL(dbo.OHEM.middleName, '') + '' + ISNULL(dbo.OHEM.firstName, '') AS EmployeeName
FROM
    dbo.`@EIM_PROCESS_DATA`
        INNER JOIN
    dbo.OHEM ON dbo.`@EIM_PROCESS_DATA`.U_Employee_ID = dbo.OHEM.empID
        CROSS JOIN
    dbo.OADM
GROUP BY dbo.`@EIM_PROCESS_DATA`.U_Tax_year , dbo.`@EIM_PROCESS_DATA`.U_Employee_ID , dbo.OADM.CompnyName , dbo.OADM.CompnyAddr , dbo.OADM.TaxIdNum , dbo.OHEM.lastName , dbo.OHEM.firstName , dbo.OHEM.middleName , dbo.`@EIM_PROCESS_DATA`.U_PD_code

的MySQL

SELECT DISTINCT 
     processdata.taxYear, processdata.empID, 
     CASE WHEN processdata.edCode = 'SYS033' THEN SUM (processdata.amount) END AS PAYE, 
     CASE WHEN processdata.edCode = 'SYS014' THEN SUM (processdata.amount) END AS TOTALTAXABLE, 
     company.companyName, company.streetAddress, company.companyPIN, employeemaster.lastName + ', ' + IFNULL(employeemaster.middleName, '') 
     + ' ' + IFNULL(employeemaster.firstName, '') AS EmployeeName
FROM         
     processdata INNER JOIN
     employeemaster ON processdata.empID = employeemaster.empID 
     CROSS JOIN company
     GROUP BY processdata.taxYear, processdata.empID, company.companyName, company.streetAddress, company.companyPIN, 
     employeemaster.lastName, employeemaster.firstName, employeemaster.middleName, processdata.edCode

3 个答案:

答案 0 :(得分:36)

MySQL等同于ISNULLIFNULL

  

如果expr1不为NULL,则IFNULL()返回expr1;否则它会返回   表达式2。

也许还要看SQL NULL Functions

The ISNULL from MySQL is used to check if a value is null

  

如果expr为NULL,则ISNULL()返回1,否则返回0.

答案 1 :(得分:2)

我建议您切换到ANSI标准函数coalesce()

(dbo.OHEM.lastName + ', ' + coalesce(dbo.OHEM.middleName, '') + '' + coalesce(dbo.OHEM.firstName, '')
) AS EmployeeName

您还可以通过包含表别名来使查询更容易阅读。

编辑:

作为一个说明,我似乎错过了转换的方向。 MySQL查询将使用concat()

CONCAT(OHEM.lastName, ', ', coalesce(OHEM.middleName, ''),
       coalesce(concat(' ', OHEM.firstName), '')
      ) AS EmployeeName

答案 2 :(得分:0)

针对具有ISNULL(value)的查询运行JUnit测试时遇到错误,错误消息为ISNULL需要两个参数。我通过将查询更改为value is null来解决此问题,并且代码现在可以正常工作,而测试也可以正常工作。