问题
I'd like to access the name of the currently executing procedure in Snowflake in the javascript procedure itself and store it in a variable.
When I interrogate the "this" object, I can see the name in the Variant return, but in terms of JSON I believe it's the name not the value and I'm not sure how to retrieve the first name in the JSON object.
What code do I need to use to get the procedure name?
procName = ???what code goes here with the this object???;
Do we know that the first name/value pair in "this" will always be the procedure name?
CREATE OR REPLACE PROCEDURE EDW_ADMIN.DAG_TEST()
RETURNS VARIANT
LANGUAGE JAVASCRIPT
AS
$$
var procName = "";
// procName = this.??? WHAT DO I PUT HERE ???
return this;
$$
;
The return / contents of "this" are:
{
"DAG_TEST": {},
"ResultSet": {},
"SfTimestamp": {},
"Snowflake": {},
"Statement": {},
"Status": {
"EOF": "eof",
"ERROR": "error",
"INITIALIZED": "initialized",
"SUCCESS": "success"
},
"_c_snowflake": {
"createExecError": {},
"createStatement": {}
},
"createError": {},
"extractValue": {},
"getColSqlTypeFromIdx": {},
"nativeTypes": {
"values": {
"BOOLEAN": "boolean",
"BUFFER": "buffer",
"DATE": "date",
"INVALID": "invalid",
"JSON": "json",
"NUMBER": "number",
"STRING": "string"
}
},
"noSuchColumnIdxErrorMessage": "Given column name/index does not exist: ",
"snowflake": {
"createStatement": {},
"execute": {}
},
"sqlTypeFromLibSfDbTypeVal": {},
"sqlTypes": {
"isArray": {},
"isBinary": {},
"isBoolean": {},
"isDate": {},
"isNumber": {},
"isObject": {},
"isText": {},
"isTime": {},
"isTimestamp": {},
"isTimestampLtz": {},
"isTimestampNtz": {},
"isTimestampTz": {},
"isVariant": {},
"values": {
"ARRAY": {
"libSfDbType": 9,
"name": "ARRAY"
},
"BINARY": {
"libSfDbType": 10,
"name": "BINARY"
},
"BOOLEAN": {
"libSfDbType": 12,
"name": "BOOLEAN"
},
"DATE": {
"libSfDbType": 3,
"name": "DATE"
},
"FIXED": {
"libSfDbType": 0,
"name": "FIXED"
},
"INVALID_SQL_TYPE": {
"libSfDbType": -1,
"name": "INVALID_SQL_TYPE"
},
"OBJECT": {
"libSfDbType": 8,
"name": "OBJECT"
},
"REAL": {
"libSfDbType": 1,
"name": "REAL"
},
"TEXT": {
"libSfDbType": 2,
"name": "TEXT"
},
"TIME": {
"libSfDbType": 11,
"name": "TIME"
},
"TIMESTAMP_LTZ": {
"libSfDbType": 4,
"name": "TIMESTAMP_LTZ"
},
"TIMESTAMP_NTZ": {
"libSfDbType": 5,
"name": "TIMESTAMP_NTZ"
},
"TIMESTAMP_TZ": {
"libSfDbType": 6,
"name": "TIMESTAMP_TZ"
},
"VARIANT": {
"libSfDbType": 7,
"name": "VARIANT"
}
}
},
"testFunc": {},
"typeFromLibSfDbTypeVal": {},
"validateBinds": {},
"validateColumnExists": {}
}
回答1:
Try this in your proc:
const procName = Object.keys(this)[0];
Assuming that the procName is always the first key in the dictionary! Also using const wherever possible is a good practice.
来源:https://stackoverflow.com/questions/59296207/snowflake-how-do-i-retrieve-the-name-of-the-currently-executing-procedure