martinson
Databricks Partner

EXECUTE IMMEDIATE requires the parameter to either be a string or a variable. You are trying to pass an expression through, causing the error. I believe that if you set up a SET VAR with the expression and then pass that through, that should solve your issue. Here is an example of what that could look like.

Let me know if that works or the error it returns

DECLARE claim_year STRING;

SET VAR claim_year = (
  SELECT CAST(CLAIM_YEAR - 1 AS STRING)
  FROM dbengineering_prod.claimscommercial.clms_commercial_ref_years R
  WHERE R.ANALYSIS_TYPE_ID = 1
    AND R.YEAR_RANK = 1
);

DECLARE sql_str STRING;

SET VAR sql_str = 'SELECT 1
   FROM `dbengineering_prod.claimscommercial.Claims_total_' || claim_year || '`
   LIMIT 10';

EXECUTE IMMEDIATE sql_str;