Snowflake SQL更新(如果存在)

发布于 2025-02-13 23:28:35 字数 785 浏览 0 评论 0原文

我看过其他问题,但似乎雪花在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2025-02-20 23:28:35

要完整插入/更新,最好使用单个合并语句

我看过Merge,但似乎不适合我,因为我的数据不是表

这不是一个问题,因为来源可能是一个表或子查询:

MERGE INTO {self.okr_table} 
USING (SELECT PARSE_JSON({json.dumps(self.data)} AS data
             , MONTH(current_date()) AS month
             , YEAR(current_date()) AS year
      ) s
  ON  {self.okr_table}.KPI
  AND MONTH({self.okr_table}.month) = s.month
  AND YEAR({self.okr_table}.month) = s.year
WHEN MATCHED THEN UPDATE
WHEN NOT MATCHED THEN INSER ...;

如果/否则在雪花中起分支有效:

BEGIN
  IF (EXISTS (...)) THEN
      UPDATE ... ;
  ELSE
      INSERT ... ;
  END IF;
END;

请注意;在每个语句之后,结束和围绕条件的括号。

To perfrom INSERT/UPDATE it is better to use single MERGE statement

I have looked at merge but it doesn't seem to fit me since my data isn't a table

It is not an issue as source could be a table or subquery:

MERGE INTO {self.okr_table} 
USING (SELECT PARSE_JSON({json.dumps(self.data)} AS data
             , MONTH(current_date()) AS month
             , YEAR(current_date()) AS year
      ) s
  ON  {self.okr_table}.KPI
  AND MONTH({self.okr_table}.month) = s.month
  AND YEAR({self.okr_table}.month) = s.year
WHEN MATCHED THEN UPDATE
WHEN NOT MATCHED THEN INSER ...;

IF/ELSE branching works in Snowflake:

BEGIN
  IF (EXISTS (...)) THEN
      UPDATE ... ;
  ELSE
      INSERT ... ;
  END IF;
END;

Please note ; after each statement, END IF and parenthesis around condition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文