‘m trying to not use the LAST_QUERY_ID approach (which used in the 2nd snippet)
WHEN I checked the documentation, i found that we can use the EXECUTE IMMEDIATE :query USING (var1,var2..) [https://docs.snowflake.com/en/sql-reference/sql/execute-immediate#executing-a-statement-with-bind-variables]
In the below code (part of stored procedure, some how it was not working.
In the first IF condition, I tried using the EXECUTE IMMEDIATE :query USING(). Second condition I used the last_query_id() which worked.
IF (:DataReadType = 'inc') THEN
-- Check datetime column exists in the table
LET qry_1 VARCHAR;
qry_1 := 'SELECT IFF(COUNT(*) > 0, TRUE, FALSE)
FROM ' || :DatabaseName || '.INFORMATION_SCHEMA.COLUMNS
WHERE table_catalog = UPPER(?)
AND table_schema = UPPER(?)
AND table_name = UPPER(?)
AND column_name IN (''__DATE_TIME'', ''__DATETIME'', )';
DateColumnExists := (EXECUTE IMMEDIATE :qry_1 USING :DatabaseName, :SchemaName, :MVName));
IF (:DateColumnExists) THEN
EXECUTE IMMEDIATE 'SELECT COLUMN_NAME
FROM '||DatabaseName || '.INFORMATION_SCHEMA.COLUMNS
WHERE table_catalog = UPPER('||'''||DatabaseName||'''||')
AND table_schema = UPPER('||'''||SchemaName||'''||')
AND table_name = UPPER('||'''||MVName||'''||')
AND column_name IN (''__DATE_TIME'', ''__DATETIME'', )
LIMIT 1';
SELECT $1 INTO :DatetimeColumn FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));
END IF;