【发布时间】:2021-11-29 05:54:51
【问题描述】:
我需要创建一个函数来根据用户的输入转换单个列的值。我需要一些关于这样做的语法方面的帮助。
这是我当前为获取行而执行的查询:
SELECT payment_id, rental_id, amount FROM payment
我正在尝试做的一些伪代码:
function getReport(String currencyType){
if(currencyType == 'EUR'){
Multiply the values in the amounts column by 1.16 and append Euros to it
Return all the rows in the table
}else if(currencyType == 'RMB'){
Multiple the values in the amounts column by 6.44 and append RMB to it
Return all the rows in the table
}else{
Do nothing because the default column values are in USD
Return all the rows in the table
}
}
我一直在尝试创建一个,但我在语法上遇到了困难。
不工作:
CREATE OR REPLACE FUNCTION get_data(currency_type text) RETURNS TABLE payment_info AS $$
CASE currency_type
WHEN 'EUR' THEN
SELECT payment_id, rental_id, amount * 1.16 FROM payment;
WHEN 'RMB' THEN
SELECT payment_id, rental_id, amount * 6.44 FROM payment;
WHEN 'USD' THEN
SELECT payment_id, rental_id, amount FROM payment;
$$ LANGUAGE SQL;
有人可以帮助我了解创建此函数的语法吗?
【问题讨论】:
-
您最好阅读CASE。提示你不能嵌套
SELECT。
标签: sql postgresql function set-returning-functions create-function