| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-04-10 : 08:53:50
|
Jon writes "This is what I have so far.CREATE PROCEDURE Random_Images ---(@Rand_1 int = 0,@Rand_2 int = 0,@Rand_3 int = 0,@Rand_4 int = 0,@Rand_5 int = 0,@Rand_6 int = 0,@Rand_7 int = 0,@Rand_8 int = 0,@Random int=0,@i int=0,@MIN int = 0,@MAX int = 0)---AS---SET @MIN = (SELECT MIN(pID) FROM Products)SET @MAX = (SELECT MAX(pID) FROM Products)-- ** Guessing from here toLOOPSET @Random = (rand()* (@MAX - @MIN)) + @MINIF @Random = (SELECT pID FROM Products WHERE pID = @Random AND Show = 'YES')SET @i = @i + 1SET @Rand_(@i) = @RandomEND IFIF @i = 8EXITEND IFEND LOOP-- ** HereSELECT * FROM ProductsWHERE pID IN (@Rand_1,@Rand_2,@Rand_3,@Rand_4,@Rand_5,@Rand_6,@Rand_7,@Rand_8)GO---Am I close or off the mark?Thank you for any help. " |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-04-10 : 09:27:03
|
| SET @Rand_(@i) = @Randomwill be a problem.You are trying to use it like an array which doesn't exist in sql server.Make it a temp table with a single row, insert the random nos and use the temp table in the in clause.(I haven't looked at what you're trying to do just how you're doing it).==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2002-04-10 : 14:47:50
|
| You might want to read this article on random rows by Graz.------------------------GENERAL-ly speaking... |
 |
|
|
motokevin
Starting Member
36 Posts |
Posted - 2002-04-10 : 19:37:07
|
| Dude,If you want 8 random records/products/images to show then try this:Select Top 8 *From ProductsWhere Show = 'Yes'Order By NewID() |
 |
|
|
jpchambers
Starting Member
3 Posts |
Posted - 2002-04-10 : 21:16:58
|
| Thank you for the ideas.nr - I will try a that and re-post.AjarnMark - I read it thanks.motokevin - Will the code take a random ID and sort down from there. I have multiple products from multiple companies. It needs to pick and choose from the entire range of ID's. Is this possible with your code?jc |
 |
|
|
motokevin
Starting Member
36 Posts |
Posted - 2002-04-10 : 21:31:42
|
| By using:ORDER BY NewID()You can select from any range of columns and the order will change each time.Try it. I'm sure you'll like it. |
 |
|
|
jpchambers
Starting Member
3 Posts |
Posted - 2002-04-11 : 00:30:35
|
| I will try it now.jc |
 |
|
|
jpchambers
Starting Member
3 Posts |
Posted - 2002-04-11 : 17:01:17
|
| Well both the solutions seem to work.motokevin - have you noticed any patterns with NewID()?(you were right an easy solution)Here is the code for the second way.Note: I was unable to figure out CASE so I used IF...END instead.CREATE PROCEDURE Random_Images2 (@Rand_1 int = 0, @Rand_2 int = 0, @Rand_3 int = 0, @Rand_4 int = 0, @Rand_5 int = 0, @Rand_6 int = 0, @Rand_7 int = 0, @Rand_8 int = 0, @Random int=0, @i int=0, @MIN int = 0, @MAX int = 0) AS SET @MIN = (SELECT MIN(pID) FROM Products) SET @MAX = (SELECT MAX(pID) FROM Products) CREATE TABLE #TmpTbl (StyleID varchar(12) NULL )INSERT INTO #TmpTbl VALUES(0)WHILE @i <> 8BEGINSET @Random = (rand()* (@MAX - @MIN)) + @MIN IF @Random = (SELECT pID FROM Products WHERE pID = @Random AND Show = 'YES')BEGINSET @i = @i + 1 UPDATE #TmpTbl SET StyleID = @RandomENDIF @i = 1BEGINSET @Rand_1 = (SELECT StyleID FROM #TmpTbl)ENDIF @i = 2BEGINSET @Rand_2 = (SELECT StyleID FROM #TmpTbl)ENDIF @i = 3BEGINSET @Rand_3 = (SELECT StyleID FROM #TmpTbl)ENDIF @i = 4BEGINSET @Rand_4 = (SELECT StyleID FROM #TmpTbl)ENDIF @i = 5BEGINSET @Rand_5 = (SELECT StyleID FROM #TmpTbl)ENDIF @i = 6BEGINSET @Rand_6 = (SELECT StyleID FROM #TmpTbl)ENDIF @i = 7BEGINSET @Rand_7 = (SELECT StyleID FROM #TmpTbl)ENDIF @i = 8BEGINSET @Rand_8 = (SELECT StyleID FROM #TmpTbl)ENDENDDROP TABLE #TmpTbl SELECT * FROM Products WHERE pID IN (@Rand_1, @Rand_2, @Rand_3, @Rand_4, @Rand_5, @Rand_6, @Rand_7, @Rand_8)GOIs this try better?Thank you all.jc |
 |
|
|
yakoo
Constraint Violating Yak Guru
312 Posts |
Posted - 2002-04-11 : 20:49:45
|
| You learn something new everyday.NewID() is pretty neat and works cool in this situation when you want to get random records from a database.Graz - you should add that into this articles (http://www.sqlteam.com/item.asp?ItemID=896)depending on the number of records you want you TOP statement would then reflect that.Very nice solution motokevinGot SQL? |
 |
|
|
motokevin
Starting Member
36 Posts |
Posted - 2002-04-11 : 20:58:12
|
| Pulling a random recordset is one of those things that you spend hours trying to solve, looking through book after book of SQL and hounding the search engines for a simple solution. Only to find Max...Min...Ave...Count...Blah...Blah...etc... I finally found this.Wish I could say I thought of it first. |
 |
|
|
|