MSCEWI4006

The selection of the variable may have runtime errors.

This is a deprecated version of the SnowConvert documentation, please visit the official site HERE.

Severity

Low

Description

This warning is added when a transformation is being applied fo a SELECT @local_variable, and the assigned expression is referencing other local variables.

Code Examples

Input Code:

DECLARE @VAR1 int;
select @VAR1 = col1 + @VAR1 + col2 from table1;

Output Code:

var EXEC = (stmt,binds = [],noCatch = false,catchFunction = null) => {
   // Some implementation here.
};
   
var INTO = function (args) {
   var result = [];
   if (ROW_COUNT && (args instanceof Array && args.length == _ROWS.getColumnCount())) {
      result = Array.apply(null,Array(_ROWS.getColumnCount()))
      while ( _ROWS.next() ) {
          result = result.map((currentValue,i) => args[i](currentValue,_ROWS.getColumnValue(i + 1)))
      }
   }
   return result;
};

let VAR1;
// ** MSC-WARNING - MSCEWI4006 - SELECTION OF @VAR1 MAY HAVE RUNTIME ERRORS **
EXEC(`SELECT col1 + ? + col2, from PUBLIC.table1`,[VAR1]);
var VAR1_OPERATION = (currentValue,nextValue) => nextValue;
[VAR1] = INTO([VAR1_OPERATION]);

Input Code:

DECLARE @VAR1 int;
DECLARE @VAR2 int;
select @VAR1 = col1 + col2, @VAR2 = @VAR1 + col3 from table1;Output Code

Output Code:

var EXEC = (stmt,binds = [],noCatch = false,catchFunction = null) => {
   // Some implementation here.
};
   
var INTO = function (args) {
   var result = [];
   if (ROW_COUNT && (args instanceof Array && args.length == _ROWS.getColumnCount())) {
      result = Array.apply(null,Array(_ROWS.getColumnCount()))
      while ( _ROWS.next() ) {
          result = result.map((currentValue,i) => args[i](currentValue,_ROWS.getColumnValue(i + 1)))
      }
   }
   return result;
};

let VAR1;
let VAR2;
// ** MSC-WARNING - MSCEWI4006 - SELECTION OF @VAR1 MAY HAVE RUNTIME ERRORS **
EXEC(`SELECT col1 + col2, ? + col3 from PUBLIC.table1`,[VAR1]);
var VAR1_OPERATION = (currentValue,nextValue) => nextValue;
var VAR2_OPERATION = (currentValue,nextValue) => nextValue;
[VAR1,VAR2] = INTO([VAR1_OPERATION,VAR2_OPERATION]);

Recommendations

  • For example 1, notice that you are using an implicit += operator, by adding the current variable value in each iteration. In the output code you should do only two changes:

    • Remove the question mark and the bindings from the EXEC call.

    • Modify the VAR1_OPERATION function, in order to sum all the values in each iteration when it is called. For example:

var VAR1_OPERATION = (currentValue,nextValue) => currentValue ? currentValue + nextValue : nextValue;
  • For example 2, notice that you are referencing @VAR1 from the same statement in VAR2 . In this case, you can simply replace the @VAR1 reference with its expression assignment, in this case col1 + col2

  • If you need more support, you can email us at snowconvert-support@snowflake.com

Last updated