PostgreSQL:获取输入参数作为行值
PostgreSQL: get input parameters as row values
我创建了一个简单的函数:
create function my fucnction(uuid, uuid, date) returns boolean as
$$
select ... from t where t.f1 = and t.f2 = and t.f3 = ;
$$
language sql stable;
如果我可以将输入参数作为单行访问(在我的函数中等于 (, , )
),那么在语法方面会很棒,所以我可以这样写:
create function my fucnction(uuid, uuid, date) returns boolean as
$$
select ... from t where (t.f1, t.f2, t.f3) = <the input parameters row>;
$$
language sql stable;
等于:
create function my fucnction(uuid, uuid, date) returns boolean as
$$
select ... from t where (t.f1, t.f2, t.f3) = (, , );
$$
language sql stable;
这可能吗?
这对我有用。我是 row()
函数的忠实粉丝,尤其是在 count(distinct row())
查询中。
create OR REPLACE function my_function(text,text,text) returns bigint as
$$
select count(*) from t where row(t1,t2,t3) = row(,,);
$$
language sql stable;
select * from my_function('a','b','c');
注意:这似乎也适用于您的 (t1,t2,t3)
语法。
编辑: 我可能误解了你的问题。如果你特别想要像触发器中那样的命名引用,你总是可以传入一个记录类型(这里我使用 table t
定义的类型)。如果您需要更复杂的函数,可能值得研究 record
datatype 以及 PLPGSQL:
create OR REPLACE function my_function(blast t) returns bigint as
$$
select count(*) from t where (t1,t2,t3) = blast;
$$
language sql stable;
select * from my_function(row('a','b','c'));
最后一个 alternative - 没有给出上面语法的好处,大概是因为类型没有定义,但是如果你需要在输入上单独的参数:
create OR REPLACE function my_function(_1 text, _2 text, _3 text) returns bigint as
$$
select count(*) from t where (t1,t2,t3) = (my_function._1, my_function._2, my_function._3);
$$
language sql stable;
select * from my_function('a','b','c')
我创建了一个简单的函数:
create function my fucnction(uuid, uuid, date) returns boolean as
$$
select ... from t where t.f1 = and t.f2 = and t.f3 = ;
$$
language sql stable;
如果我可以将输入参数作为单行访问(在我的函数中等于 (, , )
),那么在语法方面会很棒,所以我可以这样写:
create function my fucnction(uuid, uuid, date) returns boolean as
$$
select ... from t where (t.f1, t.f2, t.f3) = <the input parameters row>;
$$
language sql stable;
等于:
create function my fucnction(uuid, uuid, date) returns boolean as
$$
select ... from t where (t.f1, t.f2, t.f3) = (, , );
$$
language sql stable;
这可能吗?
这对我有用。我是 row()
函数的忠实粉丝,尤其是在 count(distinct row())
查询中。
create OR REPLACE function my_function(text,text,text) returns bigint as
$$
select count(*) from t where row(t1,t2,t3) = row(,,);
$$
language sql stable;
select * from my_function('a','b','c');
注意:这似乎也适用于您的 (t1,t2,t3)
语法。
编辑: 我可能误解了你的问题。如果你特别想要像触发器中那样的命名引用,你总是可以传入一个记录类型(这里我使用 table t
定义的类型)。如果您需要更复杂的函数,可能值得研究 record
datatype 以及 PLPGSQL:
create OR REPLACE function my_function(blast t) returns bigint as
$$
select count(*) from t where (t1,t2,t3) = blast;
$$
language sql stable;
select * from my_function(row('a','b','c'));
最后一个 alternative - 没有给出上面语法的好处,大概是因为类型没有定义,但是如果你需要在输入上单独的参数:
create OR REPLACE function my_function(_1 text, _2 text, _3 text) returns bigint as
$$
select count(*) from t where (t1,t2,t3) = (my_function._1, my_function._2, my_function._3);
$$
language sql stable;
select * from my_function('a','b','c')