Oracle函数到Snowflake函数转换

问题描述 投票:0回答:1

我正在尝试将以下 Oracle 函数转换为 Snowflake 函数: 在这方面需要一些帮助,因为我对雪花不太熟悉。

我尝试了一些函数来转换下面的代码以使其与雪花兼容,但我面临着障碍。

CREATE OR REPLACE function schema_1.getCName(CCodes VARCHAR2)
  return varchar2
as
  TYPE results_type IS REF CURSOR;
  results results_type;
  CValue VARCHAR2(1023 CHAR);
  CCodesForInClause VARCHAR2(511 CHAR) DEFAULT '';
  CCodesValue VARCHAR2(510 CHAR);
  cursorQuery VARCHAR2(1023 CHAR) DEFAULT '';
  CNames VARCHAR2(1023 CHAR);
begin
  CNames := '';
  IF(instr(CCodes,',') > 0) THEN
    CCodesValue := CCodes;
    WHILE (instr(CCodesValue,',') > 0)
    LOOP
      CCodesForInClause := CCodesForInClause || '''' || 
        trim(substr(CCodesValue, 0, instr(CCodesValue,',')-1)) || 
        ''',';
      CCodesValue := substr(CCodesValue, instr(CCodesValue,',') + 1, 
        LENGTH(CCodesValue) - instr(CCodesValue,','));
    END LOOP;

    CCodesForInClause := CCodesForInClause || '''' || trim(CCodesValue) || '''';
    cursorQuery := 'SELECT DISTINCT C_NAME FROM Table1 WHERE C_ABBREV IN ' || '(' ||  CCodesForInClause || ') order by C_NAME';

    OPEN results FOR cursorQuery;
    LOOP
      IF (length(trim(CValue)) > 0) THEN
        CNames := CNames || CValue || ';';
      END IF;
      FETCH results INTO CValue;
      EXIT WHEN results%NOTFOUND;
    END LOOP;

    IF (length(trim(CNames)) > 0) THEN
      CNames := substr(CNames, 1, length(CNames)-1);
    END IF;
    CLOSE results;
  ELSE
    OPEN results FOR 'SELECT DISTINCT C_NAME FROM Table1 WHERE C_ABBREV = :cc' USING CCodes;
    FETCH results into CValue;

      IF (results%FOUND) THEN
        CNames := CValue;
      END IF;
      CLOSE results;
    END IF;

    DBMS_Output.Put_Line(CNames);
    return CNames;
end;
oracle snowflake-cloud-data-platform
1个回答
0
投票
RETURNS STRING
LANGUAGE SQL
AS
$$
DECLARE
 results_type CURSOR;
    countryValue STRING;
    countryCodesForInClause STRING DEFAULT '';
    countryCodesValue STRING;
    cursorQuery STRING;
    countryNames STRING DEFAULT '';
    resset RESULT_SET;
     ;
BEGIN
DECLARE results_type CURSOR;
   IF(regexp_instr(countryCodes,',') > 0) THEN
        countryCodesValue := countryCodes;

    WHILE (instr(countryCodesValue,',') > 0)
    LOOP
      countryCodesForInClause := countryCodesForInClause || '''' || trim(substr(countryCodesValue, 0, regexp_instr(countryCodesValue,',')-1)) || ''',';
      countryCodesValue := substr(countryCodesValue, instr(countryCodesValue,',') + 1, LENGTH(countryCodesValue) - instr(countryCodesValue,','));
    END LOOP;

    countryCodesForInClause := countryCodesForInClause || '''' || trim(countryCodesValue) || '''';
    cursorQuery := 'SELECT DISTINCT COUNTRY_NAME FROM EmergingMarketsRegions WHERE COUNTRY_ABBREV IN ' || '(' ||  countryCodesForInClause || ') order by COUNTRY_NAME'
    ;
OPEN results_type FOR cursorQuery;
LOOP
    IF (length(trim(countryValue)) > 0) THEN
       countryNames := countryNames || countryValue || ';';
      END IF;
    FETCH results_type INTO countryValue;
    --EXIT WHEN results%NOTFOUND;
END LOOP;  
IF (length(trim(countryNames)) > 0) THEN
      countryNames := substr(countryNames, 1, length(countryNames)-1);
    END IF;
    ELSE
    cursorQuery := 'SELECT DISTINCT COUNTRY_NAME FROM ONESRC_DEV.ONESRC_OWNER.EmergingMarketsRegions where WHERE COUNTRY_ABBREV = :cc';
    resset := (EXECUTE IMMEDIATE :cursorQuery);
        -- Fetch the result
        IF (RESULTSET_HAS_ROWS(resset)) THEN
            FETCH resset INTO countryValue;
            IF (LENGTH(TRIM(countryValue)) > 0) THEN
                countryNames := countryValue;
            END IF;
        END IF;
        END IF;
        return countryNames;
        END;$$;

        call ONESRC_DEV.ONESRC_OWNER.CountryNme('USA');```
© www.soinside.com 2019 - 2024. All rights reserved.