Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Implementing a function within a stored procedure Expand / Collapse
Author
Message
Posted Monday, February 1, 2010 9:56 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 11, 2011 4:33 PM
Points: 17, Visits: 44
How do I get my Stored Procedure to use a function? The function takes in four parameters...amount, current currency, currency to convert to, and todays date...I am trying to use the function which does currency conversion to be used within my procedure to convert from one currency to another. For instance, am in the code below, am trying to use a function to convert from US dollar to CA dollar. My challenge in implementing the function in this stored procedure since the function already works.

Stored Procedure:

Create Procedure [dbo].[TSP_PricingItem]
@Price_Group VarChar(254),
@From_Item VarChar(31),
@To_Item VarChar(31),
@Price_Level VarChar(254)
As
--
Set NoCount On

/*****************************************************************************
* Reset parameter fields
*****************************************************************************/
If @From_Item = 'ALL'
Set @From_Item = '00000000000000000000'

If @To_Item = 'ALL'
Set @To_Item = 'ZZZZZZZZZZZZZZZZZZZZ'

-- Create Table Variable and populate with list of Price Groups to report
Declare @tmp VarChar(254)

Declare @PriceGroupTbl Table (PriceGroup VarChar(11))

If @Price_Group = 'ALL' -- Insert all price levels from the master table
Insert @PriceGroupTbl (PriceGroup)
Select PriceGroup From tbl09000
Else
Begin
-- loop to get all but last one when there is more than one object
While CharIndex ( ',', @Price_Group) > 0
Begin
Set @tmp = SubString(@Price_Group, 1, CharIndex ( ',', @Price_Group) - 1)
Set @tmp = LTrim(@tmp)
Insert @PriceGroupTbl (PriceGroup) Values (@tmp)
Set @Price_Group = Ltrim(SubString(@Price_Group, CharIndex ( ',', @Price_Group) + 1, Len(@Price_Group) - CharIndex ( ',', @Price_Group)))
Continue
End
-- add last one
Insert @PriceGroupTbl (PriceGroup) Values (@Price_Group)
End

-- Create Table Variable and populate with list of Price Levels to report
Declare @PriceLevelTbl Table (PriceLevel VarChar(11))

If @Price_Level = 'ALL' -- Insert all price levels from the master table
Insert @PriceLevelTbl (PriceLevel)
Select PrcLevel From tbl08000
Else
Begin
-- loop to get all but last one when there is more than one object
While CharIndex ( ',', @Price_Level) > 0
Begin
Set @tmp = SubString(@Price_Level, 1, CharIndex ( ',', @Price_Level) - 1)
Set @tmp = LTrim(@tmp)
Insert @PriceLevelTbl (PriceLevel) Values (@tmp)
Set @Price_Level = Ltrim(SubString(@Price_Level, CharIndex ( ',', @Price_Level) + 1, Len(@Price_Level) - CharIndex ( ',', @Price_Level)))
Continue
End
-- add last one
Insert @PriceLevelTbl (PriceLevel) Values (@Price_Level)
End

/*****************************************************************************
* Final Select
*****************************************************************************/
--
Select T0.ItemNmbr As ItemNumber,
T0.ItemDesc As Description,
T0.ItmShNam As ShortName,
Case T0.ItemType
When 1 Then 'Sales Inventory'
When 1 Then 'Sales Inventory'
When 2 Then 'Discontinued'
When 3 Then 'Kit'
When 4 Then 'Misc Charges'
When 5 Then 'Services'
When 6 Then 'Flat Fee'
Else 'Unknown'
End As ItemType,
T0.ItmGeDsc As GenericDescription,
T0.StndCost As StandardCost,
T0.ITEMTYPE AS ItemType,
T0.CurrCost As CurrentCost,
T0.ItemShWt As ShippingWeight,
T0.ItmClscd As ItemClass,
T0.UOMSchdl As UOMScheduleID,
T0.UsCatVls_1 As CountryOfOrigin,
T0.PrcLevel As DefaultPriceLevel,
T0.PriceGroup As PriceGroup,
T0.PrchsUOM As PurchasingUOM,
T0.SelngUOM As SellingUOM,
T2.PrcLevel AS PriceLevel,
T2.UOMPrice AS UOMPRICE,
T3.ItmClsDc As ItemClassDescription,

-- For the other columns, want to take the two rows (USD, CAD) and merge into one. So separate them into buckets
-- and then do aggregates with a group by on all the other columns - this will effectively merge the two rows into one
(Case T1.CurncyID
When 'CAD' Then T1.ListPrce
Else 0.00000
End) As CADListPrice,
(Case T1.CurncyID
When 'USD' Then T1.ListPrce
Else 0.00000
End) As USDListPrice,
Max(Case T1.CurncyID
When 'CAD' Then T2.UofM
Else ''
End) As CADPricingUOM,
Max(Case T1.CurncyID
When 'USD' Then T2.UofM
Else ''
End) As USDPricingUOM,
(Case T1.CurncyID
When 'CAD' Then Round(T1.ListPrce * (T2.UOMPrice / 100), 3)
Else 0.00000
End) As CADPrice,
(Case T1.CurncyID
When 'USD' Then Round(T1.ListPrce * (T2.UOMPrice / 100), 3)
Else 0.00000
End) As USDPrice,

(Case T1.CurncyID
When'USD' Then Round(T1.ListPrce * (T2.UOMPrice / 100), 3)
else 0.0000
End) AS USDTSCBUPrice,

(CASE T1.CurncyID
When 'CAD' Then Round(T1.ListPrce * (T2.UOMPrice / 100), 3)
else 0.0000
End) AS CADTSCBUPrice,

(Case T1.CurncyID
When 'USD' Then Round(T1.ListPrce * (T2.UOMPrice / 100), 3)
else 0.0000
End) AS USDMsrpPrice,

(Case T1.CurncyID
When 'CAD' Then Round(T1.ListPrce * (T2.UOMPrice / 100), 3)
else 0
End) AS CADMsrpPrice,

(Case T1.CurncyID
When 'USD' Then Round(T1.ListPrce * (T2.UOMPrice / 100), 3)
else 0.0000
End) AS USDMsrpDISCPrice,

(CASE T1.CurncyID
When 'CAD' Then Round(T1.ListPrce * (T2.UOMPrice / 100), 3)
else 0.0000
End) AS CADMsrpDISCPrice,
Case T0.ITEMTYPE
WHEN 2 THEN 'Yes'
ELSE 'No'
END AS Discontinued

From tbl001010 T0

Join tbl001050 T1 On
T0.ItemNmbr = T1.ItemNmbr

Join tbl001080 T2 On
T0.ItemNmbr = T2.ItemNmbr And
T0.UOMSchdl = T2.UofM And
T1.CurncyID = T2.CurncyID

Join tbl04000 T3 On
T0.ItmClscd = T3.ItmClscd

Where T0.PriceGroup In (Select PriceGroup From @PriceGroupTbl) And
T2.PrcLevel In (Select PriceLevel From @PriceLevelTbl) And
T0.Itemnmbr Between @From_Item And @To_Item


Group By T0.ItemNmbr, T0.ItemDesc, T0.ItmShNam, T0.ItemType, T0.ItmGeDsc,
T0.StndCost, T0.CurrCost, T0.ItemShWt, T0.ItmClscd, T0.UOMSchdl,
T0.UsCatVls_1, T0.PrcLevel, T0.PriceGroup, T0.PrchsUOM, T0.SelngUOM,
T2.PrcLevel, T2.UOMPRICE, T3.ItmClsDc, T1.ListPrce, T1.CurncyID

Order By T0.PriceGroup, T0.ItemNmbr, T2.PrcLevel


GO

GRANT EXECUTE ON [dbo].[TSP_PricingItem] TO [CRGRP] AS [dbo]
GO

GRANT EXECUTE ON [dbo].[TSP_PricingItem] TO [DYNGRP] AS [dbo]
GO



Post #857348
Posted Monday, February 1, 2010 10:02 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:58 AM
Points: 3,165, Visits: 8,088
How would you use the function outside the stored procedure? Have you tried doing the same inside the stored procedure? Inside or outside should make a difference.



Alvin Ramard
Memphis PASS Chapter

All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.
Post #857355
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse