![](/img/trans.png)
[英]Can I get the ID/name of database that a stored procedure is called from?
[英]Can I somehow get the value of query id of the stored procedure currently called inside that stored procedure itself in Snowflake
假設我調用這樣的存儲過程:
call SP_TEST('CAT','LION');
現在發布成功運行后,我使用以下命令獲取查詢 ID:
select last_query_id();
這將返回查詢 ID 為 01a07606-0b02-362d-0001-1d6602361072
假設我希望在運行時將此查詢 id 寫入變量 - 例如:
create or replace procedure test_proc111
("SRC" VARCHAR(30), "TGT" VARCHAR(30))
returns varchar
language javascript
execute as owner
as '
{
var TABLE_VALUE = "";
var code1 = "select last_query_id();"
var code1_excecute = snowflake.execute({sqlText: code1});
while(code1_excecute.next()) {
var TABLE_VALUE = code1_excecute.getColumnValue(1);
}
return TABLE_VALUE
}
';
現在我這樣稱呼這個存儲過程:
call test_proc1111('TEST','TARGET')
但我得到這個錯誤:
存儲過程 TEST_PROC1111 中的執行錯誤:在雪花中找不到語句 NULL。 執行,第 8 行 position 31
我們如何實現這個用例?
在問題的存儲過程中,如果它作為調用者執行,您只會獲得一個查詢 ID:
create or replace procedure test_proc111
("SRC" VARCHAR(30), "TGT" VARCHAR(30))
returns varchar
language javascript
execute as caller
as '
var TABLE_VALUE;
var code1 = "select last_query_id(-1);"
var code1_excecute = snowflake.execute({sqlText: code1});
while(code1_excecute.next()){
TABLE_VALUE = code1_excecute.getColumnValue(1);
}
return TABLE_VALUE
'
;
這是因為如果在execute as owner
,則只有在該過程中執行了另一個查詢時,它才會具有先前的查詢 ID:
create or replace procedure test_proc111
("SRC" VARCHAR(30), "TGT" VARCHAR(30))
returns varchar
language javascript
execute as owner
as '
var TABLE_VALUE;
var code1 = "select last_query_id(-1);"
var code1_excecute = snowflake.execute({sqlText: code1});
var code1 = "select last_query_id(-1);"
var code1_excecute = snowflake.execute({sqlText: code1});
while(code1_excecute.next()){
TABLE_VALUE = code1_excecute.getColumnValue(1);
}
return TABLE_VALUE
'
;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.