Jag har följande sp för att returnera ett slumpmässigt recordset.
Alter PROCEDURE getRandomAd
AS
declare @nRecordCount int
declare @nRandNum int
-- Create a temporary table with the same structure of
-- the table we want to select a random record from
CREATE TABLE #TempTable
(
strImageSrc varchar(255),
intId int identity(1,1)
)
-- Dump the contents of the table to search into the temp. table
INSERT INTO #TempTable
Select strImageSrc From tblAd_ad Where intId_adc = 1 And bitActive = 1
-- 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 strImageSrc From #TempTable
Where intId = @nRandNum
Set objConn = Server.CreateObject("ADOdb.Connection")
Set objRS = Server.CreateObject("ADOdb.Recordset")
With objConn
.Open strConn
.Execute("getRandomAd")
.Close
End With
intId = objRS(0)
strImageSrc = objRS(1)
objRS.Close
Set objRS = Nothing
Set objConn = Nothing
Jag får då felmeddelandet
Item cannot be found in the collection corresponding to the requested name or ordinal.
.
Kör jag den i InterDev står det bara
3 rows affected by last query
och inget recordset returneras.
Vad är fel? Är det överhuvudtaget ett bra sätt att hämta ett slumpmässigt valt recordset?
Något mycket alltså. Om du inte har allför många bilder så är det enklare att plocka upp alla bildnamn till en array med getrows och sedan slumpa ett tal där
randomize
set rs = "select strImageSrc from tablAd_ad"
if not rs.eof then
a = rs.getrows()
item = cint(rnd()*(ubound(a,2)+1))
end if
ungefär.
Anledningen till ditt fel är att insert skapar ett tomt och stängt recordset som returneras till ditt ASP-skript. Värdet av selecten kommer i nästa recordset.
Tack. Och om jag vill tilldela varje post en "tyngd" som anger hur många fler gånger posten skall returneras i en stor mängd än en post med tyngden 1, hur gör jag då egentligen?
137 ms totalt · 3 externa anrop · v20260731065814-full.4bcf49fe