Select Random row

I want to create a function that selects rows based on the given criteria, then from those selected rows, I want it to randomly select one of them to return.
johnywhiteAsked:
Who is Participating?
 
Aneesh RetnakaranConnect With a Mentor Database AdministratorCommented:
johnywhite,
> How would I create the entire function?

Here is the procedure,

CREATE PROCEDURE retRecords
AS
BEGIN
     select top1 * from tableName order by newID()
END


you can convert this to a function like this

CREATE FUNCTION udfUserRecords (
@Condition  varchar(100)
)
RETURNS @tab Table (i int , j varchar(1000)) ---- u need to modify this accordingly
AS
BEGIN
    INSERT INTO @tab
    SELECT TOP 1 i, j
    FROM urMasterTable
    WHERE i = @condition

    RETURn

END
0
 
Raynard7Commented:
select top1 * from tableName order by rand()
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
select top1 * from tableName order by newID()
0
 
johnywhiteAuthor Commented:
How would I create the entire function?
0
All Courses

From novice to tech pro — start learning today.