首页 > 解决方案 > Where 子句语句中的动态 LIKE

问题描述

我一直在努力实现 LIKE 语句的动态查询

从下面的 SQL 查询中,而不是对每个值都执行 ilike,它可能会变大。我无法为新的 table_name 值一次又一次地重写查询。我可以将这些值存储在单独的表中,但是如何动态实现它

  SELECT
        t.table_name as table_name,
        t.table_schema as table_schema
    FROM
       information_schema.tables T
    WHERE  (table_schema  ilike 'stage' and table_name  like 'ABC%') or (table_schema  ilike 'stage' and table_name  like 'EFG%');

我可以有另一个表,其中包含如下值列表

创建或替换表 tempdw.blk_table;( db_name varchar, tbl_expr varchar );

插入 tempdw.blk_table 值('stage','ABC%');插入 tempdw.blk_table 值('stage','EFG%');

从 tempdw.blk_table 中选择 *;

在此处输入图像描述

标签: sqlsnowflake-cloud-data-platformsnowflake-schema

解决方案


如果您只是在寻找一组带前缀的表,您可以使用regexp_like()

where table_schema ilike 'stage' and
      regexp(lower(table_name), '^abc|efg')

推荐阅读