Implementing a function within a stored procedure

  • 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_GroupVarChar(254),

    @From_ItemVarChar(31),

    @To_ItemVarChar(31),

    @Price_LevelVarChar(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@TmpVarChar(254)

    Declare@PriceGroupTblTable (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@PriceLevelTblTable (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

    *****************************************************************************/

    --

    SelectT0.ItemNmbrAs ItemNumber,

    T0.ItemDescAs Description,

    T0.ItmShNamAs 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'

    EndAs ItemType,

    T0.ItmGeDscAs GenericDescription,

    T0.StndCostAs StandardCost,

    T0.ITEMTYPEAS ItemType,

    T0.CurrCostAs CurrentCost,

    T0.ItemShWtAs ShippingWeight,

    T0.ItmClscdAs ItemClass,

    T0.UOMSchdlAs UOMScheduleID,

    T0.UsCatVls_1As CountryOfOrigin,

    T0.PrcLevelAs DefaultPriceLevel,

    T0.PriceGroupAs PriceGroup,

    T0.PrchsUOMAs PurchasingUOM,

    T0.SelngUOMAs SellingUOM,

    T2.PrcLevelAS PriceLevel,

    T2.UOMPriceAS UOMPRICE,

    T3.ItmClsDcAs 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'

    ENDAS Discontinued

    Fromtbl001010 T0

    Jointbl001050 T1 On

    T0.ItemNmbr = T1.ItemNmbr

    Jointbl001080 T2 On

    T0.ItemNmbr = T2.ItemNmbrAnd

    T0.UOMSchdl = T2.UofMAnd

    T1.CurncyID = T2.CurncyID

    Jointbl04000 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

  • 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[/url]

    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.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply