I have kind of a long sproc that is used to select the list of threads for a forum system similiar to this one. It is pretty complex because it handles pagination and ordering by a user selectable column either ascending or descending.The code below is most of the proc that I am currently using. I have never liked how I have to write the entire insert statement twice within the proc to allow the user to order by a particular column ascending or descending. Now I want to add the following additional where clause only when a variable called @gen is not equal to 0.Currently I have:WHERE fr.forum_id = @forum_id AND latest_post_date > DATEADD(dd,-@date_filter,GETDATE())But only when @gen <> 0 I want to have:WHERE fr.forum_id = @forum_id AND latest_post_date > DATEADD(dd,-@date_filter,GETDATE()) AND fr.generation = @genSo there are basically 4 different queries depending on the combination of @gen and @order1) gen = 0 and @order = 'asc'2) gen = 0 and @order = 'desc'3) gen <> 0 and @order = 'asc'4) gen <>0 and @order = 'desc'Would I be better off just going back to a dynamic query, using a long If statement or is there some simple solution I am missing here? CREATE TABLE [dbo].[#forum_temp]( [temp_id] [int] IDENTITY (1, 1) NOT NULL, [root_id] [int])IF LOWER(@order) = 'asc' BEGIN INSERT INTO [dbo].[#forum_temp] ( root_id ) SELECT fr.root_id FROM forum_root fr WHERE fr.forum_id = @forum_id AND latest_post_date > DATEADD(dd,-@date_filter,GETDATE()) ORDER BY CASE WHEN @col = 'subject' THEN fr.subject WHEN @col = 'gen' THEN RIGHT('0'+convert(varchar, fr.generation), 2) WHEN @col = 'nickname' THEN fr.nickname WHEN @col = 'replies' THEN RIGHT('000000'+convert(varchar, fr.num_children_absolute), 6) WHEN @col = 'views' THEN RIGHT('000000'+convert(varchar, fr.views), 6) WHEN @col = 'thread start date' THEN CONVERT(varchar(30),fr.thread_start_date,121) ELSE CONVERT(varchar(30),fr.latest_post_date,121) END ASC, fr.thread_start_date DESC ENDELSE BEGIN INSERT INTO [dbo].[#forum_temp] ( root_id ) SELECT fr.root_id FROM forum_root fr WHERE fr.forum_id = @forum_id AND latest_post_date > DATEADD(dd,-@date_filter,GETDATE()) ORDER BY CASE WHEN @col = 'subject' THEN fr.subject WHEN @col = 'gen' THEN RIGHT('0'+convert(varchar, fr.generation), 2) WHEN @col = 'nickname' THEN fr.nickname WHEN @col = 'replies' THEN RIGHT('000000'+convert(varchar, fr.num_children_absolute), 6) WHEN @col = 'views' THEN RIGHT('000000'+convert(varchar, fr.views), 6) WHEN @col = 'thread start date' THEN CONVERT(varchar(30),fr.thread_start_date,121) ELSE CONVERT(varchar(30),fr.latest_post_date,121) END DESC, fr.thread_start_date DESC ENDSELECT fr.root_id ... ,fr.generationFROM [dbo].[#forum_temp] temp_tblLEFT JOIN forum_root fr ON temp_tbl.root_id = fr.root_idWHERE temp_tbl.temp_id BETWEEN (@page_size * (@page_num - 1))+1 AND (@page_size * @page_num)ORDER BY temp_tbl.temp_id ASCThanks,ChrisEdited by - cbrinson on 06/18/2002 17:01:11