[英]How to write If - else if - else if - else in snowflake stored procedure
[英]Snowflake IF ELSE Stored Procedure logic
我有一個 tsql 代碼,我正在嘗試將其遷移到 Snowflake 上的存儲過程中。 我有一個版本 1 的 proc 構建和生產。 但是現在必須根據 IF-ELSE 邏輯進行查找更改。 我可以按照 T-sql 的建議輕松完成,但如何在 Snowflake 上完成相同的操作。 下面是我希望轉換為雪花的 TSQL 端代碼。
if object_id ('tempdb..#CTE_Y_Date') is not null drop table #CTE_Y_Date;
if object_id ('tempdb..#CTE_PY_Date') is not null drop table #CTE_PY_Date;
declare @week53_check int
,@Cal_Date int = 20210101
select @week53_check = Calendar_Week FROM calendar where cast([Calendar_Date] as varchar) = @Cal_Date
--print @week53_check
SELECT [Calendar_Date]
,[Fiscal_Year]
,[Fiscal_Period]
,[Calendar_Week]
,[Fiscal_Period_Week]
,[Fiscal_Period_Day]
,[Begnning_Date_PV]
,Fiscal_Quarter = case when Fiscal_Period between 1 and 3 then 1
when Fiscal_Period between 4 and 6 then 2
when Fiscal_Period between 7 and 9 then 3
when Fiscal_Period between 10 and 12 then 4 else 0 end
into #CTE_Y_Date
FROM calendar
where cast([Calendar_Date] as varchar) = @Cal_Date
-- select * from #CTE_Y_Date
if @week53_check = 53
(
SELECT a.[Calendar_Date]
,a.[Fiscal_Year]
,a.[Fiscal_Period]
,a.[Calendar_Week]
,a.[Fiscal_Period_Week]
,a.[Fiscal_Period_Day]
--,[Fiscal_Date]
-- ,[Begnning_Date]
,a.[Begnning_Date_PV]
,Fiscal_Quarter = case when a.Fiscal_Period between 1 and 3 then 1
when a.Fiscal_Period between 4 and 6 then 2
when a.Fiscal_Period between 7 and 9 then 3
when a.Fiscal_Period between 10 and 12 then 4 else 0 end
into #CTE_PY_Date
FROM calendar a
left join #CTE_Y_Date b on a.[Fiscal_Year] = b.[Fiscal_Year] and a.[Fiscal_Period_Day] = b.[Fiscal_Period_Day]
where a.[Fiscal_Year] = b.[Fiscal_Year] and a.Calendar_Week = 1
)
else insert into #CTE_PY_Date
SELECT a.[Calendar_Date]
,a.[Fiscal_Year]
,a.[Fiscal_Period]
,a.[Calendar_Week]
,a.[Fiscal_Period_Week]
,a.[Fiscal_Period_Day]
--,[Fiscal_Date]
-- ,[Begnning_Date]
,a.[Begnning_Date_PV]
,Fiscal_Quarter = case when a.Fiscal_Period between 1 and 3 then 1
when a.Fiscal_Period between 4 and 6 then 2
when a.Fiscal_Period between 7 and 9 then 3
when a.Fiscal_Period between 10 and 12 then 4 else 0 end
FROM calendar a
left join #CTE_Y_Date b on a.Fiscal_Period = b.Fiscal_Period and a.Fiscal_Period_Week = b.Fiscal_Period_Week and a.[Fiscal_Period_Day] = b.[Fiscal_Period_Day]
where a.[Fiscal_Year] = b.[Fiscal_Year] - 1
select * from #CTE_PY_Date
雪花代碼版本 1 在這里。 我需要在這里從 Tsql 上面完成 IF 塊。
CREATE or replace procedure "SP_53WeeksTest"()
returns FLOAT
language javascript
as
$$
//drop temp tables
var dtt_CTE_Y_Date = 'drop table if exists CTE_Y_Date';
var dtt_CTE_Y_Date_stmt = snowflake.createStatement({sqlText: dtt_CTE_Y_Date});
var dtt_CTE_Y_Date_res = dtt_CTE_Y_Date_stmt.execute();
var dtt_CTE_PY_Date = 'drop table if exists CTE_PY_Date';
var dtt_CTE_PY_Date_stmt = snowflake.createStatement({sqlText: dtt_CTE_PY_Date});
var dtt_CTE_PY_Date_res = dtt_CTE_PY_Date_stmt.execute();
var cttCTE_Y_Date = `create temporary table CTE_Y_Date (CALENDAR_DATE NUMBER(38,0)
,FISCAL_YEAR NUMBER(38,0)
,FISCAL_PERIOD NUMBER(38,0)
,CALENDAR_WEEK NUMBER(38,0)
,FISCAL_PERIOD_WEEK NUMBER(38,0)
,FISCAL_PERIOD_DAY NUMBER(38,0)
,BEGNNING_DATE_PV DATE
,Fiscal_Quarter NUMBER(38,0)
)`;
var cttCTE_Y_Date_stmt = snowflake.createStatement({sqlText: cttCTE_Y_Date});
var cttCTE_Y_Date_res= cttCTE_Y_Date_stmt.execute();
var CTE_Y_Date_insert = `insert into CTE_Y_Date
select CALENDAR_DATE,FISCAL_YEAR,FISCAL_PERIOD,CALENDAR_WEEK,FISCAL_PERIOD_WEEK,FISCAL_PERIOD_DAY,BEGNNING_DATE_PV
,case when Fiscal_Period between 1 and 3 then 1
when Fiscal_Period between 4 and 6 then 2
when Fiscal_Period between 7 and 9 then 3
when Fiscal_Period between 10 and 12 then 4 else 0 end as Fiscal_Quarter
from FISCAL_CALENDAR
where BEGNNING_DATE_PV = (select max(BUSINESS_DATE) from RAW_TABLE_DB.POS_TABLES.PS_TRANS_HEADER)`;
var CTE_Y_Date_insert_stmt = snowflake.createStatement({sqlText: CTE_Y_Date_insert});
var CTE_Y_Date_res= CTE_Y_Date_insert_stmt.execute();
var cttCTE_PY_Date = `create temporary table CTE_PY_Date (CALENDAR_DATE NUMBER(38,0)
,FISCAL_YEAR NUMBER(38,0)
,FISCAL_PERIOD NUMBER(38,0)
,CALENDAR_WEEK NUMBER(38,0)
,FISCAL_PERIOD_WEEK NUMBER(38,0)
,FISCAL_PERIOD_DAY NUMBER(38,0)
,BEGNNING_DATE_PV DATE
,Fiscal_Quarter NUMBER(38,0)
)`;
var cttCTE_PY_Date_stmt = snowflake.createStatement({sqlText: cttCTE_PY_Date});
var cttCTE_PY_Date_res= cttCTE_PY_Date_stmt.execute();
var CTE_PY_Date_insert = `insert into CTE_PY_Date
select A.CALENDAR_DATE,A.FISCAL_YEAR,A.FISCAL_PERIOD,A.CALENDAR_WEEK,A.FISCAL_PERIOD_WEEK,A.FISCAL_PERIOD_DAY,A.BEGNNING_DATE_PV
,case when A.Fiscal_Period between 1 and 3 then 1
when A.Fiscal_Period between 4 and 6 then 2
when A.Fiscal_Period between 7 and 9 then 3
when A.Fiscal_Period between 10 and 12 then 4 else 0 end as Fiscal_Quarter
from FISCAL_CALENDAR AS A
LEFT JOIN CTE_Y_Date AS B ON A.FISCAL_PERIOD=B.FISCAL_PERIOD AND A.FISCAL_PERIOD_WEEK=B.FISCAL_PERIOD_WEEK AND A.FISCAL_PERIOD_DAY=B.FISCAL_PERIOD_DAY
WHERE A.FISCAL_YEAR = (B.FISCAL_YEAR - 1)`;
var CTE_PY_Date_insert_stmt = snowflake.createStatement({sqlText: CTE_PY_Date_insert});
var CTE_PY_Date_res= CTE_PY_Date_insert_stmt.execute();
return 0;
$$
T-SQL 存儲過程是自上而下的流程。 雪花存儲過程(目前)是 JavaScript,應該模塊化。
要查找單個值並將其存儲在變量中,您需要使用這樣的輔助函數:
function getSingleValueQuery(columnName, queryString) {
var out;
cmd1 = {sqlText: queryString};
stmt = snowflake.createStatement(cmd1);
var rs;
rs = stmt.execute();
rs.next();
return rs.getColumnValue(columnName);
return out;
}
這使得執行重復性任務變得更容易和更具可讀性,例如在您只期望單行返回時查看運行查詢並返回結果。
然后,您可以調用該函數並將結果存儲在一個變量中,具體取決於您的數據結構:
var calDate = 20210101;
var week53_check = getSingleValueQuery('CALENDAR_WEEK',
`select CALENDAR_WEEK from CALENDAR where CALENDAR_DATE = ${calDate}`);
那么它只是在 week53_check 的條件下設置一個 if-then 塊的問題,如下所示:
if (week53_check == 53) {
// Do this
} else
// Do something else
}
為了獲得最佳可讀性,我建議使用輔助函數模塊化您的代碼:
if (week53_check == 53) {
doWeek53Insert();
} else
doNonWeek53Insert();
}
然后你可以有兩個這樣的函數:
function doWeek53Insert() {
var out;
cmd1 = {sqlText: `insert into CTE_Y_Date... (etc)... `};
stmt = snowflake.createStatement(cmd1);
var rs;
rs = stmt.execute();
}
我認為使用一些幫助程序有助於更輕松地將代碼移動到雪花 javascript。 這是我使用的一個常見片段:
// -------------- EXEC SNIPPET BEGIN --------------
var _RS, ROW_COUNT, _ROWS, MESSAGE_TEXT, SQLCODE, SQLSTATE, ERROR_HANDLERS, ACTIVITY_COUNT, INTO;
var fixBind = (arg) => arg instanceof Date && arg.toISOString() || arg || null;
var fetch = (count,rows,stmt) =>
(count && rows.next() && Array.apply(null,Array(stmt.getColumnCount())).map((_,i) => rows.getColumnValue(i + 1))) || [];
var INTO = () => (ROW_COUNT && fetch(ROW_COUNT,_ROWS,_RS)) || [];
var EXEC = function (stmt,binds,noCatch) {
try {
binds = (binds && binds.map(fixBind)) || binds;
_RS = snowflake.createStatement({sqlText : stmt,binds : binds});
_ROWS = _RS.execute();
ROW_COUNT = _RS.getRowCount();
ACTIVITY_COUNT = _RS.getNumRowsAffected();
return { INTO: () => INTO() };
}
catch(error) {
MESSAGE_TEXT = error.message;
SQLCODE = error.code;
SQLSTATE = error.state;
if (!noCatch && ERROR_HANDLERS) ERROR_HANDLERS(error);
var newError = Error(`SQLCODE: ${SQLCODE} MESSAGE: ${MESSAGE_TEXT}`)
throw newError;
}
};
// -------------- EXEC SNIPPET END --------------
我經常使用它,以至於我創建了一個簡單的工具: https : //github.com/orellabac/SnowJS-Helpers/ ,它允許我擴展該代碼以進行部署:
例如,從上面獲取您的代碼:
CREATE or replace procedure SP_53WeeksTest()
returns FLOAT
language javascript
as
$$
"@USING_EXEC";
EXEC(`DROP TABLE IF EXISTS tempdb."#CTE_Y_Date";`)
EXEC(`DROP TABLE IF EXISTS tempdb."#CTE_PY_Date";`)
var week53_check = 0;
var Cal_Date = 20210101;
EXEC(`select Calendar_Week FROM calendar where cast(Calendar_Date as varchar) = ?`,[Cal_Date])
[week53_check] = INTO();
//print @week53_check
EXEC(`
INSERT INTO "CTE_Y_Date"
SELECT Calendar_Date
,Fiscal_Year
,Fiscal_Period
,Calendar_Week
,Fiscal_Period_Week
,Fiscal_Period_Day
,Begnning_Date_PV
,Fiscal_Quarter = case when Fiscal_Period between 1 and 3 then 1
when Fiscal_Period between 4 and 6 then 2
when Fiscal_Period between 7 and 9 then 3
when Fiscal_Period between 10 and 12 then 4 else 0 end
FROM calendar
where cast(Calendar_Date as varchar) = ?]`,[Cal_Date]);
// select * from #CTE_Y_Date
if (week53_check == 53 )
{
EXEC(`
insert into "#CTE_PY_DATE"
SELECT a.Calendar_Date
,a.Fiscal_Year
,a.Fiscal_Period
,a.Calendar_Week
,a.Fiscal_Period_Week
,a.Fiscal_Period_Day
--,Fiscal_Date
-- ,Begnning_Date
,a.Begnning_Date_PV
,Fiscal_Quarter = case when a.Fiscal_Period between 1 and 3 then 1
when a.Fiscal_Period between 4 and 6 then 2
when a.Fiscal_Period between 7 and 9 then 3
when a.Fiscal_Period between 10 and 12 then 4 else 0 end
FROM calendar a
left join "#CTE_Y_Date" b on a.Fiscal_Year = b.Fiscal_Year and a.Fiscal_Period_Day = b.Fiscal_Period_Day
where a.Fiscal_Year = b.Fiscal_Year and a.Calendar_Week = 1
)`,[]);
}
else {
EXEC(`insert into "#CTE_PY_Date"
SELECT a.Calendar_Date
,a.Fiscal_Year
,a.Fiscal_Period
,a.Calendar_Week
,a.Fiscal_Period_Week
,a.Fiscal_Period_Day
--,Fiscal_Date
-- ,Begnning_Date
,a.Begnning_Date_PV
,Fiscal_Quarter = case when a.Fiscal_Period between 1 and 3 then 1
when a.Fiscal_Period between 4 and 6 then 2
when a.Fiscal_Period between 7 and 9 then 3
when a.Fiscal_Period between 10 and 12 then 4 else 0 end
FROM calendar a
left join "#CTE_Y_Date" b on a.Fiscal_Period = b.Fiscal_Period and a.Fiscal_Period_Week = b.Fiscal_Period_Week and a.Fiscal_Period_Day = b.Fiscal_Period_Day
where a.Fiscal_Year = b.Fiscal_Year - 1`);
// If you want to return this last statement
// This is want i do, I use a temporary table which I drop before use to make sure it was empty and the from the caller
// SP just select from RESULTS_SP_53WeeksTest
EXEC("DROP TABLE IF EXISTS RESULTS_SP_53WeeksTest",[]);
EXEC(`CREATE TEMPORARY TABLE RESULTS_SP_53WeeksTest AS select * from "#CTE_PY_Date"`,[]);
}
$$;
我只是使用我的工具在部署之前擴展 EXEC 代碼段,但是如果您只想復制粘貼 EXEC 代碼段而不是“@USING_EXEC”注釋
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.