0

I have an sql statement, which should contain variable value in it. but because the sql statement is coming from database retrieve, I cant do this.

my code is:

    var MySQLCommand_101 = `
        SELECT PARAM_VALUE FROM MNG.GENERAL.MNG_ELT_PARAMETERS
    `;
    var stmt = snowflake.createStatement(  {sqlText:MySQLCommand_101}  );
    var resultSet = stmt.execute();
    resultSet.next();
    var VALIDATION_RESULT = resultSet.getColumnValue(1);

now,

VALIDATION_RESULT = 'CASE WHEN DIV0(ERROR_ROWS, TOTAL_ROWS) * 100 > ${ERROR_PERCENTAGE_THRESHOLD} THEN 1 ELSE 0 END AS VALIDATION_RESULT'

in my code, I assigned ERROR_PERCENTAGE_THRESHOLD = 30. but because the VALIDATION_RESULT is coming in the execution dinamically, and did not compiled- when I return VALIDATION_RESULT I get the string itself,

CASE WHEN DIV0(ERROR_ROWS, TOTAL_ROWS) * 100 > ${ERROR_PERCENTAGE_THRESHOLD} THEN 1 ELSE 0 END AS VALIDATION_RESULT

while I'm expecting to get

CASE WHEN DIV0(ERROR_ROWS, TOTAL_ROWS) * 100 > 30 THEN 1 ELSE 0 END AS VALIDATION_RESULT
    

thanks for helping,

Riki
  • 43
  • 1
  • 7
  • did you try with VALIDATION_RESULT = ' ... \`${ERROR_PERCENTAGE_THRESHOLD}\` ' with reverse apex (ALT+96) ? – Don Diego May 11 '21 at 07:20
  • Does this answer your question? [Convert a string to a template string](https://stackoverflow.com/questions/29182244/convert-a-string-to-a-template-string) – Ivar May 11 '21 at 07:22
  • @DonDiego did not anderstand what you ment. – Riki May 11 '21 at 07:23
  • @DonDiego, your suggestion return CASE WHEN DIV0(ERROR_ROWS, TOTAL_ROWS) * 100 > ``${ERROR_PERCENTAGE_THRESHOLD}`` THEN 1 ELSE 0 END AS VALIDATION_RESULT – Riki May 11 '21 at 07:25

2 Answers2

0

If you want to have "CASE WHEN DIV0(ERROR_ROWS, TOTAL_ROWS) * 100 > 30 THEN 1 ELSE 0 END AS VALIDATION_RESULT" as VALIDATION_RESULT, you can replace the variable after fetching the value:

var ERROR_PERCENTAGE_THRESHOLD = 30;
var MySQLCommand_101 = `
    SELECT PARAM_VALUE FROM MNG.GENERAL.MNG_ELT_PARAMETERS
`;
var stmt = snowflake.createStatement(  {sqlText:MySQLCommand_101}  );
var resultSet = stmt.execute();
resultSet.next();
var VALIDATION_RESULT_TMP = resultSet.getColumnValue(1);
var VALIDATION_RESULT = VALIDATION_RESULT_TMP.replace("${ERROR_PERCENTAGE_THRESHOLD}",ERROR_PERCENTAGE_THRESHOLD ));
Gokhan Atil
  • 9,278
  • 2
  • 11
  • 24
-1

In your code

VALIDATION_RESULT = 'CASE WHEN DIV0(ERROR_ROWS, TOTAL_ROWS) * 100 > ${ERROR_PERCENTAGE_THRESHOLD} THEN 1 ELSE 0 END AS VALIDATION_RESULT'

replace the single quotes (' ') with ` `.

VALIDATION_RESULT = `CASE WHEN DIV0(ERROR_ROWS, TOTAL_ROWS) * 100 > ${ERROR_PERCENTAGE_THRESHOLD} THEN 1 ELSE 0 END AS VALIDATION_RESULT`
Ivar
  • 6,138
  • 12
  • 49
  • 61
Dhaval Darji
  • 513
  • 5
  • 19
  • I cant insert this to my database. INSERT INTO MNG_ELT_PARAMETERS VALUES (concat(`CASE WHEN DIV0(ERROR_ROWS, TOTAL_ROWS) * 100 > $`,`{ERROR_PERCENTAGE_THRESHOLD}` THEN 1 ELSE 0 END AS VALIDATION_RESULT`) I have to use concat because the ${}, and then the `` dont allow me to put * – Riki May 11 '21 at 07:31