我在 Snowflake 中注意到了一些关于有效符号的事情。
为了避免维护存储过程的重复、重载版本,重载的替代方法可能是在不需要其他值时要求传递某种可测试的虚假变体或 NULL。
-- Call the SP by passing a testable, falsy value:
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2'); -- This will fail fail without overloading with a matched, 2 string/varchar signature.
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2', NULL); -- This will work.
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2', ''::variant); -- This will work.
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2', array_construct()); -- This will work.
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2', object_construct()); -- This will work.
当然array_construct('PARAM3', 'PARAM4', 'PARAM5'))也可以写成parse_json('["PARAM3", "PARAM4", "PARAM5"]')。
同样,object_construct('PARAM3_NAME', 'PARAM3_VALUE', 'PARAM10_NAME', 'PARAM10_VALUE') 也可以写成parse_json('{"PARAM3_NAME": "PARAM3_VALUE", "PARAM10_NAME", "PARAM10_VALUE"}')。
除非您比其他两个函数更喜欢 parse_json() ,否则这些替代方案都没有给我们任何有用的东西。
另外,我不确定这是否一直有效(也许 Greg Pavlik 知道?),但是这些变体类型的符号可以通过使用{} 构造一个对象或使用[] 构造一个数组来稍微缩写,因此是变得更干净,更具可读性。
为了探索 Snowflake 将接受的符号,以下是可以工作的代码示例:
-- Call the SP using different notations:
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2', (select array_construct('PARAM3', 'PARAM4', 'PARAM5'))); -- Make the notation awkward & hard to read.
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2', (select ['PARAM3', 'PARAM4', 'PARAM5'])); -- Make the notation awkward & hard to read.
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2', ['PARAM3', 'PARAM4', 'PARAM5']); -- This also works & is easy to read.
call VARIABLE_SIGNATURE('PARAM1', 'PARAM2', {'PARAM3_NAME': 'PARAM3_VALUE', 'PARAM10_NAME': 'PARAM10_VALUE'}); -- This also works & is easy to read.