宏命令是在HQL中调用其他函数和操作符来定义函数的功能。
比较适合做分析时为一些临时需要用到很多次的繁琐表达式封装一下,取个简短点的别名以便重复调用例子
-- 宏只能是临时宏,只在本次回话中可见、有效,需要将宏脚本放在SQL脚本的头部 -- 一个根据生日推算星座的宏命令 DROP TEMPORARY MACRO IF EXIST MACRO_GET_XINGZUO; CREATE TEMPORARY MACRO MACRO_GET_XINGZUO(BIRTHDAY STRING) CASE WHEN BIRTHDAY>=‘0101‘ AND BIRTHDAY<=‘0119‘ THEN ‘魔羯座‘ WHEN BIRTHDAY>=‘0120‘ AND BIRTHDAY<=‘0218‘ THEN ‘水瓶座‘ WHEN BIRTHDAY>=‘0219‘ AND BIRTHDAY<=‘0320‘ THEN ‘双鱼座‘ WHEN BIRTHDAY>=‘0321‘ AND BIRTHDAY<=‘0420‘ THEN ‘白羊座‘ ...... WHEN BIRTHDAY>=‘1122‘ AND BIRTHDAY<=‘1221‘ THEN ‘射手座‘ WHEN BIRTHDAY>=‘1222‘ AND BIRTHDAY<=‘1231‘ THEN ‘魔羯座‘ ELSE NULL END;
之后使用: SELECT MACRO_GET_XINGZUO(BIRTHDAY) FROM TB_CUSTOMER;
参考:https://segmentfault.com/a/1190000009816609
原文:https://www.cnblogs.com/foolangirl/p/13730656.html