我使用postgresql
创建了这个存储过程:
create or replace Function GetTimeStamp(
IN t1 timestamp ,
IN t2 timestamp ,
Out reg_os Varchar(50),
Out customer_count int ,
Out t5 timestamp
) returns void AS $$
Begin
SELECT payment.payment_ts AS t , customer.registration_os As os ,count(Distinct customer.customer_id) As total
From customer inner join payment on customer.customer_id = payment.customer_id
Where Payment.payment_ts Between t1 And t2
Group By 1 , 2
Union
SELECT transfer.ts_created AS t , customer.registration_os As os, count(Distinct customer.customer_id) As total
From customer inner join account on customer.customer_id = account.customer_id inner join transfer on account.account_id = transfer.from_account
Where transfer.ts_created Between t1 And t2
Group By 1 , 2;
select t , os , total into t5 , reg_os , customer_count ;
End;
$$ LANGUAGE plpgsql;
我能够创建存储过程,但是当我使用此代码时:
SELECT GetTimeStamp('2016-01-01' ,'2017-01-01') OR
SELECT * FROM GetTimeStamp('2016-01-01' ,'2017-01-01')
它给了我一个错误:
查询没有结果数据的目的地提示:如果要丢弃 SELECT的结果,改为使用PERFORM
我在存储过程中使用了perform而不是select,它给了我
PERFORM附近的语法错误。
我不知道我是否可以在两个执行查询之间使用联合。如何获得此存储过程的结果?
答案 0 :(得分:1)
在PL / pgSQL中,您需要使用SELECT ... INTO
来指定应存储结果的变量。要丢弃结果,请使用PERFORM
代替SELECT
。
答案 1 :(得分:0)
您需要return a set (table
),而且可以是SQL
create or replace Function GetTimeStamp (
IN t1 timestamp ,
IN t2 timestamp
) returns table (
reg_os Varchar(50),
customer_count int,
t5 timestamp
) language sql as $$
SELECT
payment.payment_ts AS t ,
customer.registration_os As os ,
count(Distinct customer.customer_id) As total
From
customer
inner join
payment on customer.customer_id = payment.customer_id
Where Payment.payment_ts Between t1 And t2
Group By 1 , 2
Union
SELECT
transfer.ts_created AS t ,
customer.registration_os As os,
count(Distinct customer.customer_id) As total
From
customer
inner join
account on customer.customer_id = account.customer_id
inner join
transfer on account.account_id = transfer.from_account
Where transfer.ts_created Between t1 And t2
Group By 1 , 2;
$$;