SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Implementing a function within a stored procedure


Implementing a function within a stored procedure

Author
Message
mohaminho10
mohaminho10
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 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
Alvin Ramard
Alvin Ramard
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5285 Visits: 11657
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.

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search