Snowflake - using binding variables

1.4k Views Asked by At

I am trying to call the below procedure and I'm getting an unexpected ':' error due to binding variable. If I am using simple JS concatenation instead,its working. But please let me know if there is any alternative using binding variables.Also in which all part of a query binding variables will work?

CREATE OR REPLACE PROCEDURE test_proc ()
RETURNS STRING
LANGUAGE JAVASCRIPT
AS
$$
var V_TEMP = '123'
var V_SQL = `CREATE TABLE TEST_TABLE:1  AS
SELECT  A.FIRST_NAME,
A.LAST_NAME
FROM    
MARKET A
WHERE  first_name>3000;`
var EXEC_V_SQL = snowflake.createStatement(
{
sqlText: V_SQL, binds: [ V_TEMP ]
}
)
var result1 = EXEC_V_SQL.execute();
$$; ```
1

There are 1 best solutions below

2
On

The issue is you are using a bind variable for an object name without using the IDENTIFIER() function. Try something like this instead:

CREATE OR REPLACE PROCEDURE test_proc ()
RETURNS STRING
LANGUAGE JAVASCRIPT
AS
$$
var V_TEMP = 'TEST_TABLE123'
var V_SQL = `CREATE TABLE IDENTIFIER(:1)  AS
SELECT  A.FIRST_NAME,
A.LAST_NAME
FROM    
MARKET A
WHERE  first_name>3000;`
var EXEC_V_SQL = snowflake.createStatement(
{
sqlText: V_SQL, binds: [ V_TEMP ]
}
)
var result1 = EXEC_V_SQL.execute();
$$;