Example: create a function to get total UnitPrice of a particular OrderID from Northwind2 database
use Northwind2
go
create function getOrderTotalPrice
(
@OrderId int
)
return int
as
begin
declare @TotalPrice int
set @TotalPrice = 0
select @TotalPrice sum(UnitPrice) [Order Details] where OrderId = @OrderId
return @TotalPrice
end
Create a new stored procedure to get Order Detail from Northwind2 database
create proc selOrderDetail
(
@OrderId int
)
as
select OrderID, dbo.getOrderTotalPrice(OrderID) as TotalPrice, OrderDate, RequiredDate, ShippedDate, ShipName, ShipAddress, ShipCity,
ShipRegion, ShipPostalCode, ShipCountry
from Orders where OrderId = @OrderId
Usage: exec stored procedure selOrderDetail which takes OrderId as input parameter
exec selOrderDetail 10248