繁体   English   中英

AWS Redshift IF-ELSE 逻辑

[英]AWS Redshift IF-ELSE Logic

我基本上想在 Redshift 中做以下事情,但由于 Redshift 不支持程序语句,我不确定如何实现我的目标:

IF EXISTS (select username,accountnumber from table where username={1})
THEN
    IF {0} NOT IN accountnumber 
        update table set accountnumber = 
        accountnumber+=',{0}' where username='{1}'
    END IF
ELSE
    insert into table values({1},{0});
END IF

参数 1 是用户 ID,参数 2 是用户名

在红移中实现这一目标的最佳方法是什么? 感谢您提前提供任何帮助

跑步:

CREATE TABLE IF NOT EXISTS table (account_number 
varchar(100), username varchar(50));
case when true
update table set accountnumber = accountnumber+=',{0}' 
where username='{1}'
else insert into table values ('{0}','{1}')
end

定义 true 仅用于演示目的。 我收到以下错误:

---> syntax error at or near "case"
LINE 2: case when 1=1

我看到exists红移支持,所以也许这会起作用

/* if the row is there it'll update it. If not it won't */
UPDATE Table Set Field='NewValue' Where KeyValue=1

/* if the row is not there it'll insert it */
INSERT INTO Table (KeyValue,Field1,Field2)
SELECT 1,'NewValue','NewValue'
WHERE NOT EXISTS (SELECT * FROM Table WHERE KeyValue=1)

您需要使用与if-else类似的CASE表达式。

case when new_record = existing_record
update existing_record
else insert new_record
end

如果我没记错的话,您似乎正在尝试进行更新。 您可以在 redshift 上使用合并功能吗? 在进行插入/更新之前,您需要使用临时表来保存一些值。

以下是 redshift 文档中的一些示例

https://docs.aws.amazon.com/redshift/latest/dg/merge-examples.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM