Subject | Using Stored Procedures in a view |
---|---|
Author | kokok_kokok |
Post date | 2004-06-04T09:05:50Z |
I want to create a view that shows the results of a table.
The idea is:
create view Totals(
sales,
amount,
price
)
as select
C.CustomerId,
T.sales,
T.amount,
T.price
from Customers C, SP_Totals(Customers.CustomerId) T;
Where SP_Totals is a stored procedure. It resturns the totals by the
customer Id. This procedure is something similar to:
select count(*), sum(amount), avg(price) from orders into sales,
amount, price where CustomerId=:CustomerId;
This example does not work because according to the
documentation "Note: You cannot select from a view that is based on
the result set of a stored procedure." but it is valid to show my
idea.
I know that I can use:
create view Totals(
CustomerId,
sales,
amount,
price
)
as select C.CustomerId,
((select count(*) from orders where CustomerId=C.CustomerId)),
((select sum(amunt) from orders where CustomerId=C.CustomerId)),
((select avg(price) from orders where CustomerId=C.CustomerId))
from Customers C;
But I do know if it is efficient because there 3 selects instead of
1. Maybe Firebird optimizes it, I do not know.
This is a simplified example, in the real example there are about 20
fields, so I cannot use this second option.
My question: How can I make it without penalizing the performance?
Thanks in advanced
The idea is:
create view Totals(
sales,
amount,
price
)
as select
C.CustomerId,
T.sales,
T.amount,
T.price
from Customers C, SP_Totals(Customers.CustomerId) T;
Where SP_Totals is a stored procedure. It resturns the totals by the
customer Id. This procedure is something similar to:
select count(*), sum(amount), avg(price) from orders into sales,
amount, price where CustomerId=:CustomerId;
This example does not work because according to the
documentation "Note: You cannot select from a view that is based on
the result set of a stored procedure." but it is valid to show my
idea.
I know that I can use:
create view Totals(
CustomerId,
sales,
amount,
price
)
as select C.CustomerId,
((select count(*) from orders where CustomerId=C.CustomerId)),
((select sum(amunt) from orders where CustomerId=C.CustomerId)),
((select avg(price) from orders where CustomerId=C.CustomerId))
from Customers C;
But I do know if it is efficient because there 3 selects instead of
1. Maybe Firebird optimizes it, I do not know.
This is a simplified example, in the real example there are about 20
fields, so I cannot use this second option.
My question: How can I make it without penalizing the performance?
Thanks in advanced