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.
| 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 intdeclare @nRandNum intdeclare @LikeTeamID intdeclare @productID int-- Create a temporary table with the same structure of-- the table we want to select a random record fromCREATE TABLE #TempTable( ProdID int, TeamID int, idNum int identity(1,1) )--Get the Team ID of the main productSelect @LikeTeamID = products.TeamID FROM productsWHERE products.ProdID = @i-- Dump the contents of the table to seach into the -- temp. tableINSERT INTO #TempTableSELECT products.TeamID, products.ProdIDFROM productsWHERE products.TeamID = @LikeTeamID-- Get the number of records in our temp tableSelect @nRecordCount = count(*) From #TempTable-- Select a random number between 1 and the number-- of records in our tableSelect @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 = @nRandNumSELECT products.ProdID, products.Product_Type, products.Brand, products.Product_Name, products.Picture_ID_T, Prod_Supply.Price, Team_names.Team_Name, CitySt_Tbl.citystate_nameFROM (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.ProdIDWhere 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=350johnEdited by - theEphesian on 10/17/2001 01:09:53 |
|
|
|
|
|
|
|