Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Getting a Random Record Stored Proc. from Form

Author  Topic 

theEphesian
Starting Member

6 Posts

Posted - 2001-10-16 : 23:49:02
Hi all. Im having a problem with this.Im making an athletic apparel site. and on the product page i would like to bring back a random product that matches the same team as the main product. (example if your looking at a miami dolphins hat, the random product would bring back a miami dolphins t-shirt or jacket or what ever.) I am trying to write a stored procedure for this. the code looks like this :
quote:

CREATE PROCEDURE sp_GetRandomProduct






(
@i int
)
AS

declare @nRecordCount int
declare @nRandNum int
declare @LikeTeamID int
declare @productID int









-- Create a temporary table with the same structure of
-- the table we want to select a random record from
CREATE TABLE #TempTable
(
ProdID int,
TeamID int,
idNum int identity(1,1)
)



--Get the Team ID of the main product

Select @LikeTeamID = products.TeamID
FROM products
WHERE products.ProdID = @i



-- Dump the contents of the table to seach into the
-- temp. table

INSERT INTO #TempTable
SELECT products.TeamID, products.ProdID
FROM products
WHERE products.TeamID = @LikeTeamID










-- Get the number of records in our temp table
Select @nRecordCount = count(*) From #TempTable






-- Select a random number between 1 and the number
-- of records in our table
Select @nRandNum = Round(((@nRecordCount - 2) * Rand() + 1), 0)





-- Select the record from the temp table with the
-- ID equal to the random number selected...
Select @productID = ProdID From #TempTable
Where idNum = @nRandNum





SELECT products.ProdID, products.Product_Type, products.Brand, products.Product_Name, products.Picture_ID_T, Prod_Supply.Price, Team_names.Team_Name, CitySt_Tbl.citystate_name
FROM (Team_names INNER JOIN (CitySt_Tbl INNER JOIN products ON CitySt_Tbl.City_St_ID = products.CityStID) ON Team_names.TeamID = products.TeamID) INNER JOIN Prod_Supply ON products.ProdID = Prod_Supply.ProdID
Where products.ProdID = @productID



My problem is that it returns the wrong product every time.Some where it thinks that the ProdID is the team id.So if the main products ID is 350 and its team id is 103.it will return a productid of 103 instead of looking at all products that have a teamid of 103. So you get the wrong product.Does anyone see where i have made a Mistake?

heres the page to look at.
http://www.akolade.com/athletic/apparel/?i=350
john

Edited by - theEphesian on 10/17/2001 01:09:53
   

- Advertisement -