set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_getStoriesByTag]
@tagname varchar(2000),
@sort int
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql as varchar(2000)
DECLARE @tagcount as int
SET @tagcount = LEN(@tagname) - LEN(REPLACE(@tagname, ',', '')) + 1
SET @sql = 'SELECT DISTINCT recid, author, view_publicstories.recdate, title, storycodes, rating, ranking, views, pages
FROM view_publicstories INNER JOIN (
SELECT storyid
FROM tbl_taglink
WHERE tagid IN (
SELECT tagid
FROM tbl_tags
WHERE ( tagname in (' + @tagname + ') ) )
GROUP BY storyid
HAVING COUNT(*) = ' + CAST(@tagcount AS varchar(10)) + ') AS matches
ON (view_publicstories.recid = matches.storyid)
ORDER BY ' + CASE @sort
WHEN 1 THEN 'view_publicstories.ranking'
WHEN 2 THEN 'view_publicstories.rating'
WHEN 3 THEN 'view_publicstories.title'
ELSE '0'
END
EXEC(@sql)
|