Teradata variable with list of values

本小妞迷上赌 提交于 2020-01-11 12:54:32

问题


Is there a way to pass a list of values in a variable and use that in an IN() statement to check a field against the passed in list of values?

The only thing I can think of is something like this:

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

Thanks.


回答1:


TD14 supports a nice table function called 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');


来源:https://stackoverflow.com/questions/28905265/teradata-variable-with-list-of-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!