snowflake,获取两个表之间不匹配列的列表 (SQL)

snowflake, get a list of mismatching columns between two tables (SQL)

我一直在做一些研究,但没有找到太多。我需要比较两个 table 以获取 table 1 中但不在 table 2 中的列的列表。我正在使用 Snowflake。 现在,我找到了这个答案:

问题是当我 运行 代码时我得到这个错误:

SQL compilation error: invalid identifier TRANSIENT_STAGE_TABLE

如果我单独 运行 代码工作正常,所以如果我 运行:

SELECT column_name
FROM information_schema.columns 
WHERE table_schema = 'your_schema' AND table_name = 'table2'

我实际上得到了一个列名列表,但是当我将它链接到第二个表达式时,返回了上述错误。 关于发生了什么的任何提示? 谢谢

原始 post 中的查询应该有效,也许您在某处缺少单引号?看这个例子

create or replace table xxx1(i int, j int);
create or replace table xxx2(i int, k int);

-- Query from the original post
SELECT column_name
FROM information_schema.columns 
WHERE table_name = 'XXX1'
    AND column_name NOT IN
    (
        SELECT column_name
        FROM information_schema.columns 
        WHERE table_name = 'XXX2'
    );
-------------+
 COLUMN_NAME |
-------------+
 J           |
-------------+

您还可以编写一个稍微复杂的查询来查看两个表中所有不匹配的列:

with 
s1 as (
  select table_name, column_name 
  from information_schema.columns 
  where table_name = 'XXX1'), 
s2 as (
  select table_name, column_name 
  from information_schema.columns 
  where table_name = 'XXX2') 
select * from s1 full outer join s2 on s1.column_name = s2.column_name;
------------+-------------+------------+-------------+
 TABLE_NAME | COLUMN_NAME | TABLE_NAME | COLUMN_NAME |
------------+-------------+------------+-------------+
 XXX1       | I           | XXX2       | I           |
 XXX1       | J           | [NULL]     | [NULL]      |
 [NULL]     | [NULL]      | XXX2       | K           |
------------+-------------+------------+-------------+

您当然可以添加 WHERE s1.column_name IS NULL or s2.column_name IS NULL 以仅查找缺失的列。

您还可以轻松扩展它以检测列类型差异。