Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2007, 06:31 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default Add new Table to Stored Procedure

Hi,

I was given the task of adding a new table to a stored procedure that I did not write and cannot figure out where to add the code for it. Basically, I need to do the following:

1 - Add a new parameter to asp_GetRate stored procedure

2 - In asp_GetRate (see below), I already have links to tblAdPartInRateCard and tblRateCard. The fields I need are in tblMedia (AgencyDiscount and CashDiscount) and tblMedia links to tblRateCards via VehicleKey so I have to join these two tables to get these fields. Once I get these, I have to apply the discount i.e reducing the gross rate by 15%.

I would appreciate very much if someone could help me.

Cheers


asp_GetRate stored procedure

CREATE PROC asp_GetRate
(
@p_UserID int,
@p_RateCardID int,
@p_EarnedRateBW int,
@p_EarnedRateColour int,
@p_AdPageID int,
@p_PositionID int =0
)
AS
DECLARE @fltAdRate float
DECLARE @fltPosRate float

--CHECK FOR Missing Adparts
IF ( SELECT (COUNT(*))
 FROM tblAdUnitInAdPage adup,
  tblAdPartInAdUnit apau,
  tblAdParts adp,
  tblAdPartTypes adpt
 WHERE adPageID=@p_AdPageID
 AND adup.AdUnitID = apau.AdUnitID
 AND apau.AdPartID = adp.AdPartID
 AND adp.AdPartTypeID = adpt.AdPartTypeID
 AND apau.AdPartID NOT IN (
  SELECT AdPartID
  FROM tblAdPartInRateCard
  WHERE RateCardID=@p_RateCardID)
  AND
  ( adpt.AdPartTypeName IN ('B&W', 'Position', 'Insert','TCP')
 OR (adpt.AdPartTypeName = 'Color' AND adp.Code <> 'BW')
  )
 ) = 0
--No missing adparts
BEGIN

 SELECT @fltAdRate =SUM(Rate * adu.AdUnitCount)
 FROM tblAdPages adp
 INNER JOIN tblAdUnitInAdPage adup
 ON adp.AdPageID = adup.AdPageID
 INNER JOIN tblAdUnits adu
 ON adu.AdUnitID = adup.AdUnitID
 INNER JOIN tblAdPartInAdUnit adpu
 ON adup.AdUnitID = adpu.AdUnitID
 INNER JOIN tblAdParts adpt
 ON adpt.AdPartID = adpu.AdPartID
 INNER JOIN tblAdPartTypes adptt
 ON adpt.AdPartTypeID = adptt.AdPartTypeID
INNER JOIN tblAdPartInRateCard adptr
 ON adpu.AdPartID = adptr.AdPartID
INNER JOIN tblRateCards rat
 ON adptr.RateCardID = rat.RateCardID
 AND adptr.RateCardID = @p_RateCardID
 WHERE adp.AdPageID = @p_AdPageID
 AND adptr.EarnedRate
  =
  (SELECT MAX(adptr.EarnedRate)
   FROM tblAdPartInRateCard adptr
   WHERE adpt.AdPartID = adptr.AdPartID
   AND rat.RateCardID = adptr.RateCardID
   AND ISNULL(adptr.EarnedRate,0)<=
    CASE adptt.AdPartTypeName
   WHEN 'B&W' THEN @p_EarnedRateBW
   WHEN 'Color' THEN @p_EarnedRateColour
    WHEN 'Bleed' THEN 1
    WHEN 'Position' THEN @p_EarnedRateBW
    WHEN 'Insert' THEN @p_EarnedRateBW
    WHEN 'TCP' THEN @p_EarnedRateBW
    END
  )

 --Cost of position
 SELECT @fltPosRate = MIN(Rate)
 FROM tblAdPartInRateCard
 WHERE AdPartID = @p_PositionID
 AND RateCardID = @p_RateCardID
 AND EarnedRate <= @p_EarnedRateBW
 AND @p_PositionID > 0

 --Output total rate
 SELECT @fltAdRate + ISNULL(@fltPosRate,0) as Rate

END
ELSE
 --Missing adparts
 --Return error value
 SELECT -1
GO
 
Old February 14th, 2007, 10:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

As far adding new parameter... it should go next to
Code:
CREATE PROC asp_GetRate  
(  
@p_UserID int,  
@p_RateCardID int,  
@p_EarnedRateBW int,  
@p_EarnedRateColour int,  
@p_AdPageID int,  
@p_PositionID int =0,
<your_new_parameter> <datatype> = <default value if any>
On adding new table I dont understand what needs to go where, unless you explain in detail what actually is required...

Cheers


_________________________
- Vijay G
Strive for Perfection
 
Old February 14th, 2007, 12:58 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Vijay G,

Many thanks for your help.

It turned out that I got away by doing this:

select @cashDiscount = CashDiscount, @agencyDiscount=Agencydiscount from tblMedia m
inner join tblRateCards r on r.VehicleKey = m.VehicleKey where ratecardid= @p_RateCardID

And that got the value I need.

Cheers

CP





Similar Threads
Thread Thread Starter Forum Replies Last Post
Use Temp Table Variable in Stored Procedure rweide SQL Server ASP 2 April 15th, 2011 03:50 PM
Stored Procedure That Updates Table GailCG Classic ASP Professional 1 January 22nd, 2006 01:11 PM
Build a table from a Stored Procedure rogue248 SQL Server 2000 2 October 7th, 2004 10:06 PM
How to add a stored procedure in Access badgolfer Access 2 June 17th, 2004 08:13 PM
Trying to add a GetTaxAmount Stored Procedure attipa Classic ASP Databases 3 July 18th, 2003 03:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.