Question: Using Microsoft SQL server manager 2008.

Making a stored procedure that will "eventually" select the top 10 on the Pareto list. But I also would like to run this again to find the bottom 10.

Now, instead of replicating the query all over again, I'm trying to see if there's a way to pass a parameter into the query that will change the order by from asc to desc.

Is there any way to do this that will save me from replicating code?

```
CREATE PROCEDURE [dbo].[TopVRM]
@orderby varchar(255)
AS
SELECT Peroid1.Pareto FROM dbo.Peroid1
GROUP by Pareto ORDER by Pareto @orderby

```

Answer: 
try:

```
 CREATE PROCEDURE [dbo].[TopVRM]
(@orderby varchar(255)
AS
IF @orderby='asc'
SELECT Peroid1.Pareto FROM dbo.Peroid1
GROUP by Pareto ORDER by Pareto asc
ELSE
SELECT Peroid1.Pareto FROM dbo.Peroid1
GROUP by Pareto ORDER by Pareto desc

```
**This gives you more options**

```
CREATE PROCEDURE [dbo].[TopVRM] @orderby varchar(255) = 'Pareto asc'
DECLARE @SendIt NVARCHAR(MAX)
AS
BEGIN
    SET @SendIt = 'SELECT Peroid1.Pareto FROM dbo.Peroid1
                   GROUP by Pareto ORDER by '+ @orderby
    EXEC sp_executesql @SendIt
END
GO
EXEC dbo.TopVRM 'Pareto DESC'
GO

```