雪花立即执行到变量中
Snowflake execute immediate into variable
我有一个 table,其中一列包含我要执行的 SQL 查询。这些查询是某种质量检查(例如“select count(*) from the table where col = x”)。因此,现在我遍历 table 中的行,进行特定查询并执行它,并将该查询的实际值(结果)放入变量中。 Snowflake 脚本可以做到这一点吗?
是的,如果我通过“变量”理解你想要什么......
一些设置:
CREATE table test.test2.to_do(checks string);
INSERT INTO to_do values ('select count(*) from test.test2.to_do;');
- 现在一些 Snowflake 脚本从“检查 table”
遍历游标
- 然后运行那个SQL
- 然后将结果捕获到变量中
declare
counts int;
total_counts int := 0;
c1 cursor for select checks from test.test2.to_do;
begin
for record in c1 do
execute immediate record.checks;
select into counts from table(result_scan(last_query_id()));
total_counts := total_counts + counts;
end for;
return total_counts;
end;
anonymous block
1
加强这些检查:
INSERT INTO to_do values ('select sum(10) from test.test2.to_do;');
select * from test.test2.to_do;
CHECKS
select count(*) from test.test2.to_do;
select sum(10) from test.test2.to_do;
运行 上面的脚本 SQL 再次:
anonymous block
22
但是我们可以直接注射吗?
INSERT INTO to_do values ('select count(*) into counts from test.test2.to_do;');
INSERT INTO to_do values ('select sum(10) into counts from test.test2.to_do;');
改变方块:
declare
counts int;
total_counts int := 0;
c1 cursor for select checks from test.test2.to_do;
begin
for record in c1 do
execute immediate record.checks;
--select into counts from table(result_scan(last_query_id()));
total_counts := total_counts + counts;
end for;
return total_counts;
end;
和运行:
Error: 'STATEMENT_ERROR' on line 8 at position 4 : SQL compilation error: error line 1 at position 0 INTO clause is not allowed in this context (line 8)
NO 阻塞,所以上次查询是..
我有一个 table,其中一列包含我要执行的 SQL 查询。这些查询是某种质量检查(例如“select count(*) from the table where col = x”)。因此,现在我遍历 table 中的行,进行特定查询并执行它,并将该查询的实际值(结果)放入变量中。 Snowflake 脚本可以做到这一点吗?
是的,如果我通过“变量”理解你想要什么......
一些设置:
CREATE table test.test2.to_do(checks string);
INSERT INTO to_do values ('select count(*) from test.test2.to_do;');
- 现在一些 Snowflake 脚本从“检查 table” 遍历游标
- 然后运行那个SQL
- 然后将结果捕获到变量中
declare
counts int;
total_counts int := 0;
c1 cursor for select checks from test.test2.to_do;
begin
for record in c1 do
execute immediate record.checks;
select into counts from table(result_scan(last_query_id()));
total_counts := total_counts + counts;
end for;
return total_counts;
end;
anonymous block |
---|
1 |
加强这些检查:
INSERT INTO to_do values ('select sum(10) from test.test2.to_do;');
select * from test.test2.to_do;
CHECKS |
---|
select count(*) from test.test2.to_do; |
select sum(10) from test.test2.to_do; |
运行 上面的脚本 SQL 再次:
anonymous block |
---|
22 |
但是我们可以直接注射吗?
INSERT INTO to_do values ('select count(*) into counts from test.test2.to_do;');
INSERT INTO to_do values ('select sum(10) into counts from test.test2.to_do;');
改变方块:
declare
counts int;
total_counts int := 0;
c1 cursor for select checks from test.test2.to_do;
begin
for record in c1 do
execute immediate record.checks;
--select into counts from table(result_scan(last_query_id()));
total_counts := total_counts + counts;
end for;
return total_counts;
end;
和运行:
Error: 'STATEMENT_ERROR' on line 8 at position 4 : SQL compilation error: error line 1 at position 0 INTO clause is not allowed in this context (line 8)
NO 阻塞,所以上次查询是..