WilliamB2
WilliamB2

Reputation: 45

Teradata variable with list of values

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.

Upvotes: 0

Views: 1952

Answers (1)

dnoeth
dnoeth

Reputation: 60462

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');

Upvotes: 1

Related Questions