Snowflake SQL更新(如果存在)
我看过其他问题,但似乎雪花在SQL中不支持,至少不是其他SQL服务器支持它的方式。
一些建议使用JavaScript,但如果可以的话,我想避免使用。
我正在尝试使用Snowflake Python库插入表中的一些数据,如果它已经存在,那么我想更新数据,我已经查看了Merge,但它似乎不适合我,因为我的数据不是表
到目前为止,这就是我无法正常工作的
IF (EXISTS (SELECT * FROM {self.okr_table} WHERE kpi=TRUE AND Month(month)=MONTH(current_date()) AND year(month)=YEAR(current_date())))
THEN
UPDATE {self.okr_table} SET [DATA] = {json.dumps(self.data)} WHERE kpi=TRUE AND Month(month)=MONTH(current_date()) AND year(month)=YEAR(current_date()))
ELSE
INSERT INTO {self.okr_table} (month, data, kpi) SELECT current_date(),parse_json('{json.dumps(self.data)}'), true;
END
I have looked at other question but seems that snowflake doesn't support if/else in SQL, at least not the way that other sql servers support it.
some suggested to use JavaScript but I would like to avoid that if I can.
I am trying to Insert into a table using snowflake python library some data, if it's already there then I would like to update the data, I have looked at merge but it doesn't seem to fit me since my data isn't a table
that's what I have so far that isn't working
IF (EXISTS (SELECT * FROM {self.okr_table} WHERE kpi=TRUE AND Month(month)=MONTH(current_date()) AND year(month)=YEAR(current_date())))
THEN
UPDATE {self.okr_table} SET [DATA] = {json.dumps(self.data)} WHERE kpi=TRUE AND Month(month)=MONTH(current_date()) AND year(month)=YEAR(current_date()))
ELSE
INSERT INTO {self.okr_table} (month, data, kpi) SELECT current_date(),parse_json('{json.dumps(self.data)}'), true;
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要完整插入/更新,最好使用单个合并语句
这不是一个问题,因为来源可能是一个表或子查询:
如果/否则在雪花中起分支有效:
请注意
;
在每个语句之后,结束和围绕条件的括号。To perfrom INSERT/UPDATE it is better to use single MERGE statement
It is not an issue as source could be a table or subquery:
IF/ELSE branching works in Snowflake:
Please note
;
after each statement,END IF
and parenthesis around condition.