函数中的SQL Case语句

时间:2016-10-19 20:04:44

标签: mysql sql case

我正在尝试编写一个带有两个参数的函数,并根据case语句返回计算结果(请参阅下文)。我一直收到语法错误:

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在(medToConvert)=" Codeine"那么MME = doseToConver'在第13行

这是我迄今为止所尝试过的:

    /* Function that takes two parameters as input:
   Dosage of an opioid
   Name of the opioid

   Returns the morphine equivalent dosage */

    CREATE FUNCTION convertToMorphineEquiv (doseToConvert INT, medToConvert VARCHAR(20))
    RETURNS INT

    BEGIN
        DECLARE MME INT

        CASE       
                 when (medToConvert) = "Codeine" then MME = doseToConvert * 0.15

                 -- Fentanyl Transdermal (in mcg/hr)
                 when (medToConvert) = "Fentanyl" then MME = doseToConvert * 2.4

                 when (medToConvert) = "Hydrocodone" then MME = doseToConvert * 1
                 when (medToConvert) = "Hydromorphone" then MME = doseToConvert * 4
                 when (medToConvert) = "Methadone" AND doseToConvert BETWEEN 1 AND 20 then MME = doseToConvert * 4
                 when (medToConvert) = "Methadone" AND doseToConvert BETWEEN 21 AND 40 then MME = doseToConvert * 8
                 when (medToConvert) = "Methadone" AND doseToConvert BETWEEN 41 AND 60 then MME = doseToConvert * 10
                 when (medToConvert) = "Methadone" AND doseToConvert >=60 then MME = doseToConvert * 12
                 when (medToConvert) = "Morphine" then MME = doseToConvert * 1
                 when (medToConvert) = "Oxycodone" then MME = doseToConvert * 1.5
                 when (medToConvert) = "Oxymorphone" then MME = doseToConvert * 3
                 when (medToConvert) = "Tapentadol" then MME = doseToConvert * 0.4

                 else "Conversion for this opioid is not available"
        END

         RETURN MME
    END

1 个答案:

答案 0 :(得分:0)

改为创建一个表,然后加入它。使用CROSS APPLY操作可以获得更快的性能,因为标量值的用户定义函数会受到RBAR(行累加行)性能损失的影响。

git checkout -b tmp_fix D~1
git merge C
# now, you have correct D', but you should make it child of D to pick it
git reset --soft D
git commit -m "Fix incorrect merge D"
git checkout develop
git cherry-pick tmp_fix