Tutorial Mistake??

Results 1 to 3 of 3

Thread: Tutorial Mistake??

  1. #1
    Join Date
    Dec 1969

    Default Tutorial Mistake??

    Re: Getting a Random Record Using a Stored Procedure<BR>http://www.aspfaqs.com/webtech/081100-1.shtml<BR><BR>Shouldn&#039;t...<BR>@nRandNum = Round(((@nRecordCount - 2) * Rand() + 1), 0)<BR><BR>Be...<BR>@nRandNum = Round(((@nRecordCount - 1) * Rand() + 1), 0)<BR><BR>I would imagine there&#039;s not much impact in the difference, but I wanted to know to make sure I truly understood the code (not just cut-n-pasted it).<BR><BR><BR>Full code included here since its small<BR>==============================<BR>CREATE PROCEDURE sp_GetRandomRecord<BR>AS<BR><BR>declare @nRecordCount int<BR>declare @nRandNum int<BR><BR>-- Create a temporary table with the same structure of<BR>-- the table we want to select a random record from<BR>CREATE TABLE #TempTable<BR>(<BR> field1 varchar(255),<BR> field2 varchar(50),<BR> field3 int,<BR> idNum int identity(1,1)<BR>)<BR><BR>-- Dump the contents of the table to seach into the <BR>-- temp. table<BR>INSERT INTO #TempTable<BR>Select field1, field2, field3 From myTable<BR><BR>-- Get the number of records in our temp table<BR>Select @nRecordCount = count(*) From #TempTable<BR><BR>-- Select a random number between 1 and the number<BR>-- of records in our table<BR>Select @nRandNum = Round(((@nRecordCount - 2) * Rand() + 1), 0)<BR><BR>-- Select the record from the temp table with the<BR>-- ID equal to the random number selected...<BR>Select field1, field2, field3 From #TempTable<BR>Where idNum = @nRandNum<BR>Go<BR>===============================

  2. #2
    Join Date
    Dec 1969

    Default RE: Tutorial Mistake??

    I think this method is a little better:<BR><BR>http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=173

  3. #3
    Join Date
    Dec 1969

    Default You are correct...

    ...the "tutorial" (??) is wrong.<BR><BR>But I also agree with Dutch.<BR><BR>If you had 100,000 records in your main table, then you&#039;d be creating a 100,000 record temp table, just to throw it away. Pure yuck. Horrible use of resources.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts