I have a complex (for me) stored procedure.
This is greatly simplified.
CREATE PROCEDURE sp_newest
(@orderby varchar(10),
@orderdir varchar(4),
@search varchar(100))
AS
SELECT name,address,city,state,zip,price,date
FROM (table1 INNER JOIN table2 ON table1.id = table2.id)
WHERE (table1.status = 2)
GROUP BY name,address,city,state,zip
ORDER BY
CASE @orderdir
WHEN 'asc' THEN
CASE @orderby
WHEN 'price' THEN price
END
END
ASC,
CASE @orderdir
WHEN 'asc' THEN
CASE @orderby
WHEN 'name' THEN name
END
END
ASC,
CASE @orderdir
WHEN 'asc' THEN
CASE @orderby
WHEN 'date' THEN date
END
END
ASC,
CASE @orderdir
WHEN 'desc' THEN
CASE @orderby
WHEN 'price' THEN price
END
END
DESC,
CASE @orderdir
WHEN 'desc' THEN
CASE @orderby
WHEN 'name' THEN name
END
END
DESC,
CASE @orderdir
WHEN 'desc' THEN
CASE @orderby
WHEN 'date' THEN date
END
END
DESC
GO
I need to return only 100 records and the records need to be the 100 newest (based on date).
The user may choose to sort by price or name or date, but the records always have to be the 100 newest.
Not sure how to accomplish that.
by: _agx_Posted on 2007-06-06 at 19:10:33ID: 19229991
Use the TOP operator
p,price,da te
SELECT TOP 100 name,address,city,state,zi
FROM .....