Link to home
Start Free TrialLog in
Avatar of juliandormon
juliandormon

asked on

Create temp table while looping through cursor??

Hi There,
I am trying to view a recordset from a temporary table where the records get inserted as I do a cursor loop in a stored procedure. The problem is I am not sure if this at all the right way to accomplish what I am trying to do, but if it is, I can't seem to see any data in the temp table eventhough the proper number of records are created. Preferably I would appreciate it if you can tell me a better method for what I am trying to achieve - and I'll explain that now.

My basic query is to get a recordset of our products.
My products however belong to one of many possible categories (and is related by foreign key to a categories table).
Likewise, my products can belong to 1 of four seasons categories (also related to the Seasons table by a foreign key).
Sometimes we put an entire category of goods on sale by the Category they belong to and or by the Season they belong to.

So... As we loop through our products - we want to query the categories and season tables to see if there are any discounts which then need to be multiplied against our Products.Price field and returned to the recordset like so.

I think the logic will be clearer when you see the SQL below. I hope this makes sense.

Thanks in advance.

ALTER PROCEDURE sp_select_specials
/*
      (
            @parameter1 datatype = default value,
            @parameter2 datatype OUTPUT
      )
*/
AS

-- We don't want to return the # of rows inserted
-- into our temporary table, so turn NOCOUNT ON
SET NOCOUNT ON


--Create a temporary table
CREATE TABLE #TempItems
(
              ID int IDENTITY,
      SKU varchar(50),
              Name varchar(50),
              Description varchar(5000),
      Thumb varchar(255),
      Price money,
      SeasonID int,
      CategoryID int,
      GenderID int,
      ConditionID int
)

--cursor variables
Declare @SKU varchar(50)
Declare @Name varchar(50)
Declare @Description varchar(5000)
Declare @Thumb varchar(255)
Declare @Price money
Declare @DiscountPrice money
Declare @SeasonID int
Declare @CategoryID int
Declare @GenderID int
Declare @ConditionID int
Declare @CategoryDiscount Decimal(18,3)
Declare @SeasonDiscount Decimal(18,3)
Declare @UseThisPrice money
Declare @FinalPrice money

Declare MyCursor CURSOR FOR

SELECT SKU, Name, Description, Thumb, Price, DiscountPrice, SeasonID, CategoryID, GenderID, ConditionID
FROM Products
WHERE FeaturedItem = 1
ORDER BY Name ASC

OPEN MyCursor

FETCH NEXT FROM MyCursor
INTO @SKU, @Name, @Description, @Thumb, @Price, @DiscountPrice, @SeasonID, @CategoryID, @GenderID, @ConditionID

WHILE @@FETCH_STATUS = 0
BEGIN

      --Determine what price to use. If no discount use use price else use discount price
      If @DiscountPrice > 0
      Begin
      Set @UseThisPrice = @DiscountPrice
      End
      Else
      Begin
      Set @UseThisPrice = @Price
      End

      -- Get Category Discount
      Select @CategoryDiscount = (Select CategoryDiscount From Categories Where CategoryID = @CategoryID)

      If @CategoryDiscount > 0
      Begin
      Set @UseThisPrice = (@UseThisPrice - (@UseThisPrice * @CategoryDiscount))
      End

      -- Get Season Discount
      Select @SeasonDiscount = (Select SeasonDiscount From Seasons Where SeasonID = @SeasonID)
      If @SeasonDiscount > 0
      Begin
      Set @UseThisPrice = (@UseThisPrice - (@UseThisPrice * @SeasonDiscount))
      End


INSERT INTO #TempItems (SKU, Name, Description, Thumb, Price, SeasonID, CategoryID, GenderID, ConditionID) Values (@SKU, @Name, @Description, @Thumb, @UseThisPrice, @SeasonID, @CategoryID, @GenderID, @ConditionID)


FETCH NEXT FROM MyCursor
INTO @SKU, @Name, @Description, @Thumb, @Price, @DiscountPrice, @SeasonID, @CategoryID, @GenderID, @ConditionID
END


CLOSE MyCursor
DEALLOCATE MyCursor

Select * From #TempItems

-- Turn NOCOUNT back OFF
SET NOCOUNT OFF  

      /* SET NOCOUNT ON */
      RETURN
ASKER CERTIFIED SOLUTION
Avatar of BillAn1
BillAn1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BillAn1
BillAn1

there is no need to have nested queries. You get better perofmrance if you just do 3 multiplications.
If there is the case that sometimes there is no entry in the Seasons or Categort table etc you can use left outer join
also, if there is no need to remove any -ve numbers, you can remove the case altogether and just use the isnull function

SELECT
     p.SKU, p.Name, p.Description, p.Thumb,
     (isnull(p.DiscountPrice,Price) *
     ( 1 - isnull(CategoryDiscount,0)) *
     ( 1 - isnull(SeasonDiscount,0)),
   SeasonID,p.CategoryID, p.GenderID, p.ConditionID
FROM
Products p LEFT OUTER JOIN Categories C on p.CategoryID = c.CategoryID
LEFT OUTER JOIN Seasons s on p.SeasonID = s.seasonID
Avatar of juliandormon

ASKER

thanks Guys,
With some fiddling, both your suggestions worked but BillAn1's answer seemed like it may be faster.