Showing posts with label recipe. Show all posts
Showing posts with label recipe. Show all posts

Monday, March 12, 2012

passing a parameter to TOP when getting random data

> Dear all,
> I am trying to write a stored procedure for a recipe database so that the
> user can choose how many recipes from what category.
> I have written this one to get 7 recipes from dessert category:
>
> create proc sp_ran(
>
> @.cat varchar(30)
> )
> as
> begin
>
> SELECT TOP 7 r.name,r.method FROM recipe r join Reccat rc on
> r.ID=rc.recipeID
> join category ca on ca.ID=rc.categoryID where ca.name=@.cat
> ORDER BY NEWID()
>
> end
>
> just want to know if I want the user to decide the number of recipe, how
> can
> I pass this parameter to the TOP? so a certain number of recipe will be
> randomly selected from the table?
>
> Thanks!!

Quote:

Originally Posted by crazyfisher

> Dear all,
> I am trying to write a stored procedure for a recipe database so that the
> user can choose how many recipes from what category.
> I have written this one to get 7 recipes from dessert category:
>
> create proc sp_ran(
>
> @.cat varchar(30)
> )
> as
> begin
>
> SELECT TOP 7 r.name,r.method FROM recipe r join Reccat rc on
> r.ID=rc.recipeID
> join category ca on ca.ID=rc.categoryID where ca.name=@.cat
> ORDER BY NEWID()
>
> end
>
> just want to know if I want the user to decide the number of recipe, how
> can
> I pass this parameter to the TOP? so a certain number of recipe will be
> randomly selected from the table?
>
> Thanks!!


Have one more input variable @.num in the procedure that accepts no of rows required and make use of this @.num in your query.