小编典典

具有值列表的Teradata变量

sql

有没有一种方法可以在变量中传递值列表,并在IN()语句中使用它来对照传入的值列表检查字段?

我唯一能想到的就是这样:

SELECT
  field_name
WHERE
  (SELECT INSTR(@variable_list, field_name))>0

谢谢。


阅读 189

收藏
2021-04-14

共1个答案

小编典典

TD14支持名为STRTOK_SPLIT_TO_TABLE的漂亮表函数:

REPLACE MACRO testmac(param VARCHAR(1000)) AS
(
SELECT * FROM dbc.DatabasesV AS db
JOIN 
 (
   SELECT token AS DatabaseName, tokennum
   FROM TABLE (STRTOK_SPLIT_TO_TABLE(1, :param, ',')
        RETURNS (outkey INTEGER, -- usually the PK of the table, here it's just a dummy
                 tokennum INTEGER, -- order of the token within the param string
                 token VARCHAR(128) CHARACTER SET UNICODE)
              ) AS d 
 ) AS dt
ON db.DatabaseName  = dt.DatabaseName
ORDER BY tokennum;
);

EXEC testmac('dbc,systemfe,syslib');
2021-04-14