ASP.NET Perform Calculation in Stored Procedure, or in Databind in Gridview
Hey so I have a stored procedure populating a gridview
and in my stored procedure I have two columns such as
Total_Price DataType Money
and
Discount DateType decimal(18, 0)
and I want to display a derived column called Actual Price
which is
Total_Price - Discount
is it best to create this column in the stored procedure or calculate on load in the gridview ?
Heres my stored procedure at the moment
SELECT f.supplier_name,d.product_ID_key, d.product_name AS productName, d.packsize, d.tradePrice, d开发者_如何学运维.IPU_code, d.EAN_code, c.discount, e.stock_indicator
FROM aw_customer AS a INNER JOIN
aw_cust_contract AS b ON a.cust_ID_key = b.cust_ID_key INNER JOIN
aw_contract_line AS c ON b.contract_ID_key = c.contract_ID_key INNER JOIN
aw_product AS d ON c.product_ID_key = d.product_ID_key INNER JOIN
aw_stock AS e ON d.product_ID_key = e.product_ID_key INNER JOIN
aw_supplier AS f ON d.supplier_ID_key = f.supplier_ID_key
WHERE (a.cust_ID_key = @customerId)
ORDER BY d.product_name
I would calculate this at Stored Procedure level, so that any calls to this Stored Procedure would return the same results (you haven't got to do the math on other gridviews etc in the future if they are calling the same Stored Procedure)
In situations like this, I've often created a SQL View
so that the calculations are done within the view, and then multiple Stored Procedures can call this View and display the data like:
SELECT Total_Price
,Discount
,Actual_Price
FROM [v_TableA]
http://msdn.microsoft.com/en-us/library/aa214068%28v=sql.80%29.aspx
See this article for details of Indexing Views, which will also improve performance:
http://technet.microsoft.com/en-us/library/cc917715.aspx
Any way doing this on the data base level (in SP) will significantly improve a performance of data calculation but on other hand adding a new column increases a network load due to increased data amount.
So decide yourself what is more important for your application.
精彩评论