Variables 会话变量作为标识符

Variables 会话变量作为标识符,variables,session,snowflake-cloud-data-platform,identifier,Variables,Session,Snowflake Cloud Data Platform,Identifier,在Snowflake中使用会话变量作为标识符时遇到了一些问题。我已经审阅了文件: 我能够让他们的例子发挥作用,但在我看来,所提出的概念的逻辑应用不起作用。下面是一个有效的示例(首先将数据库和模式设置为沙盒): 输入: set (table_n, column_n) = ('test', 'c1'); select $table_n, $column_n; create or replace table identifier($table_n) (c1 number); insert i

在Snowflake中使用会话变量作为标识符时遇到了一些问题。我已经审阅了文件:

我能够让他们的例子发挥作用,但在我看来,所提出的概念的逻辑应用不起作用。下面是一个有效的示例(首先将数据库和模式设置为沙盒):

输入:

set (table_n, column_n) = ('test', 'c1');
select $table_n, $column_n;
create or replace table identifier($table_n) (c1 number);
insert into identifier($table_n) (c1) values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
insert into identifier($table_n) (identifier($column_n)) values (5), (6), (7);
create or replace table identifier($table_n) (identifier($column_n) number);
set (table_n, column_n) = ('test', '\'c1\'');
select $table_n, $column_n;
create or replace table identifier($table_n) ("'c1'" number);
insert into identifier($table_n) ("'c1'") values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
输出:

$TABLE_N    $COLUMN_N
test        c1
c1
1
2
3
SQL compilation error: syntax error line 1 at position 44 unexpected '('. syntax error line 1 at position 45 unexpected '$column_n'. syntax error line 1 at position 55 unexpected ')'.
SQL compilation error: syntax error line 1 at position 56 unexpected '('. syntax error line 1 at position 74 unexpected ')'.
$TABLE_N    $COLUMN_N
test        'c1'
number of rows inserted
3
SQL compilation error: error line 1 at position 18 invalid identifier '$column_n'
输入:

set (table_n, column_n) = ('test', 'c1');
select $table_n, $column_n;
create or replace table identifier($table_n) (c1 number);
insert into identifier($table_n) (c1) values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
insert into identifier($table_n) (identifier($column_n)) values (5), (6), (7);
create or replace table identifier($table_n) (identifier($column_n) number);
set (table_n, column_n) = ('test', '\'c1\'');
select $table_n, $column_n;
create or replace table identifier($table_n) ("'c1'" number);
insert into identifier($table_n) ("'c1'") values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
输出:

$TABLE_N    $COLUMN_N
test        c1
c1
1
2
3
SQL compilation error: syntax error line 1 at position 44 unexpected '('. syntax error line 1 at position 45 unexpected '$column_n'. syntax error line 1 at position 55 unexpected ')'.
SQL compilation error: syntax error line 1 at position 56 unexpected '('. syntax error line 1 at position 74 unexpected ')'.
$TABLE_N    $COLUMN_N
test        'c1'
number of rows inserted
3
SQL compilation error: error line 1 at position 18 invalid identifier '$column_n'
到目前为止还不错。这些都是从文件中提出来的。但是,当我尝试在稍微不同的上下文中使用标识符时,操作失败,并显示一条无用的错误消息

输入:

set (table_n, column_n) = ('test', 'c1');
select $table_n, $column_n;
create or replace table identifier($table_n) (c1 number);
insert into identifier($table_n) (c1) values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
insert into identifier($table_n) (identifier($column_n)) values (5), (6), (7);
create or replace table identifier($table_n) (identifier($column_n) number);
set (table_n, column_n) = ('test', '\'c1\'');
select $table_n, $column_n;
create or replace table identifier($table_n) ("'c1'" number);
insert into identifier($table_n) ("'c1'") values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
输出:

$TABLE_N    $COLUMN_N
test        c1
c1
1
2
3
SQL compilation error: syntax error line 1 at position 44 unexpected '('. syntax error line 1 at position 45 unexpected '$column_n'. syntax error line 1 at position 55 unexpected ')'.
SQL compilation error: syntax error line 1 at position 56 unexpected '('. syntax error line 1 at position 74 unexpected ')'.
$TABLE_N    $COLUMN_N
test        'c1'
number of rows inserted
3
SQL compilation error: error line 1 at position 18 invalid identifier '$column_n'
输入:

set (table_n, column_n) = ('test', 'c1');
select $table_n, $column_n;
create or replace table identifier($table_n) (c1 number);
insert into identifier($table_n) (c1) values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
insert into identifier($table_n) (identifier($column_n)) values (5), (6), (7);
create or replace table identifier($table_n) (identifier($column_n) number);
set (table_n, column_n) = ('test', '\'c1\'');
select $table_n, $column_n;
create or replace table identifier($table_n) ("'c1'" number);
insert into identifier($table_n) ("'c1'") values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
输出:

$TABLE_N    $COLUMN_N
test        c1
c1
1
2
3
SQL compilation error: syntax error line 1 at position 44 unexpected '('. syntax error line 1 at position 45 unexpected '$column_n'. syntax error line 1 at position 55 unexpected ')'.
SQL compilation error: syntax error line 1 at position 56 unexpected '('. syntax error line 1 at position 74 unexpected ')'.
$TABLE_N    $COLUMN_N
test        'c1'
number of rows inserted
3
SQL compilation error: error line 1 at position 18 invalid identifier '$column_n'
当标识符的开头或结尾有一个引号时,我也会出错。如果我们返回到第一个有效的示例,只需将列名更改为“'c1'”,它就会失败

输入:

set (table_n, column_n) = ('test', 'c1');
select $table_n, $column_n;
create or replace table identifier($table_n) (c1 number);
insert into identifier($table_n) (c1) values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
insert into identifier($table_n) (identifier($column_n)) values (5), (6), (7);
create or replace table identifier($table_n) (identifier($column_n) number);
set (table_n, column_n) = ('test', '\'c1\'');
select $table_n, $column_n;
create or replace table identifier($table_n) ("'c1'" number);
insert into identifier($table_n) ("'c1'") values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
输出:

$TABLE_N    $COLUMN_N
test        c1
c1
1
2
3
SQL compilation error: syntax error line 1 at position 44 unexpected '('. syntax error line 1 at position 45 unexpected '$column_n'. syntax error line 1 at position 55 unexpected ')'.
SQL compilation error: syntax error line 1 at position 56 unexpected '('. syntax error line 1 at position 74 unexpected ')'.
$TABLE_N    $COLUMN_N
test        'c1'
number of rows inserted
3
SQL compilation error: error line 1 at position 18 invalid identifier '$column_n'
输入:

set (table_n, column_n) = ('test', 'c1');
select $table_n, $column_n;
create or replace table identifier($table_n) (c1 number);
insert into identifier($table_n) (c1) values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
insert into identifier($table_n) (identifier($column_n)) values (5), (6), (7);
create or replace table identifier($table_n) (identifier($column_n) number);
set (table_n, column_n) = ('test', '\'c1\'');
select $table_n, $column_n;
create or replace table identifier($table_n) ("'c1'" number);
insert into identifier($table_n) ("'c1'") values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
输出:

$TABLE_N    $COLUMN_N
test        c1
c1
1
2
3
SQL compilation error: syntax error line 1 at position 44 unexpected '('. syntax error line 1 at position 45 unexpected '$column_n'. syntax error line 1 at position 55 unexpected ')'.
SQL compilation error: syntax error line 1 at position 56 unexpected '('. syntax error line 1 at position 74 unexpected ')'.
$TABLE_N    $COLUMN_N
test        'c1'
number of rows inserted
3
SQL compilation error: error line 1 at position 18 invalid identifier '$column_n'
输入:

set (table_n, column_n) = ('test', 'c1');
select $table_n, $column_n;
create or replace table identifier($table_n) (c1 number);
insert into identifier($table_n) (c1) values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
insert into identifier($table_n) (identifier($column_n)) values (5), (6), (7);
create or replace table identifier($table_n) (identifier($column_n) number);
set (table_n, column_n) = ('test', '\'c1\'');
select $table_n, $column_n;
create or replace table identifier($table_n) ("'c1'" number);
insert into identifier($table_n) ("'c1'") values (1), (2), (3);
select identifier($column_n) from identifier($table_n);
输出:

$TABLE_N    $COLUMN_N
test        c1
c1
1
2
3
SQL compilation error: syntax error line 1 at position 44 unexpected '('. syntax error line 1 at position 45 unexpected '$column_n'. syntax error line 1 at position 55 unexpected ')'.
SQL compilation error: syntax error line 1 at position 56 unexpected '('. syntax error line 1 at position 74 unexpected ')'.
$TABLE_N    $COLUMN_N
test        'c1'
number of rows inserted
3
SQL compilation error: error line 1 at position 18 invalid identifier '$column_n'

非常感谢您的帮助。我真的很想了解幕后发生了什么,所以我真的知道如何使用雪花。谢谢大家!

对于您的第一个SQL错误,我能够使用下面的语句将值插入表中,而不必指定列(在本例中为c1):

INSERT INTO identifier($table_n)  VALUES (5), (6), (7);
这似乎是因为C1默认为数字索引:其中选择输出为:

但是对于 创建或替换标识符(变量)标识符(变量)。。。; 似乎引起了一些问题。 Select语句中用逗号分隔的两个变量不会引发错误

我确实找到了SQL标识符的注释:

标识符(数据库名、表名、列名等)不能 以特殊字符开头,除非整个名称包含在 双引号。有关更多信息,请参阅对象标识符

起草结论:(variable variable)只能在会话变量中执行,不能在SQL变量中执行(例如在JDBCAPI中)

想法

增加: 此外,当我将最后一个select语句更改为时,它没有给出预期的结果

select $column_n from identifier($table_n);

是列“$column\n”的名称还是变量名?这是变量名。变量是通过以下方式创建的:``set variable\u name=value```然后它们被引用:``选择$variable\u name``对于第一个错误,我想您可以使用它作为一种简单的解决方法,只需为不使用的列指定空值或默认值,但它似乎仍然被破坏了。尽管如此,它还是完成了任务,所以谢谢你的想法。我不明白你的意思“(变量)只能在会话变量中执行,不能在SQL变量中执行”,你能详细说明一下吗?运行上一个select语句时发生了什么?