2

I've below Stored Procedure named GetFilteredArticles which returns 7 seperate tables for each day of a week i.e. Monday to Sunday

CREATE Procedure [dbo].[GetFilteredArticles]                                                                                                
@UserName NVARCHAR(100), 
AS                            
BEGIN                                                                  
SET NOCOUNT ON;  

[**Returns table for Monday**]
SELECT PostID,Title,convert(char(10),PostDate,101) as PostDate,IsEntertainment, ISNULL(DrPick ,0) as DrPick,                                               
   ImageURL,ISNULL(MustPick ,0) as MustPick ,ISNULL(Lead ,0) as Lead                                                                                                 
   ,ISNULL(NonLead,0) as NonLead , convert(char(10),CreationDate,101) as CreationDate,ISNULL(TBW,0) as TBW,                                        
   SortOrder,NLDate,[Day], PostURL, Direction,ISNULL(IsArticleLevelPost,0) as IsArticleLevelPost ,Sources, HeadLine,                                   
   ISNULL(PromoteTo,0) as PromoteTo,ParentID, PostLevel, IsHLTestPassed, Synapsis, ProjectedViews,ISNULL([SaveChecked],0) as [SaveChecked]                                     
   FROM  PromoteToArticleNLPrep                                      
where day='Monday' and UserName = @UserName <--Condition [where day='Monday']                                                                                                                             
order by ISNULL(SortOrder,0) asc

[**Returns table for Tuesday**]                                                                        
SELECT PostID,Title,convert(char(10),PostDate,101) as PostDate,IsEntertainment, ISNULL(DrPick ,0) as DrPick,                                                                                                     
   ImageURL,ISNULL(MustPick ,0) as MustPick ,ISNULL(Lead ,0) as Lead                                                                                                      
   ,ISNULL(NonLead,0) as NonLead , convert(char(10),CreationDate,101) as CreationDate,ISNULL(TBW,0) as TBW,                                        
   SortOrder,NLDate,[Day], PostURL, Direction  , ISNULL(IsArticleLevelPost,0) as IsArticleLevelPost,Sources, HeadLine ,                                   
   ISNULL(PromoteTo,0) as PromoteTo,ParentID, PostLevel, IsHLTestPassed, Synapsis, ProjectedViews,ISNULL([SaveChecked],0) as [SaveChecked]                                       
   FROM PromoteToArticleNLPrep                                                                                                                                                                                                                     
where day='Tuesday' and UserName = @UserName <--Condition [where day='Tuesday']                                                                                                                            
order by ISNULL(SortOrder,0) asc    

[**Returns table for Wednesday**]                                                                                                                                                                            
SELECT PostID,Title,convert(char(10),PostDate,101) as PostDate,IsEntertainment, ISNULL(DrPick ,0) as DrPick,                                                                                                     
   ImageURL,ISNULL(MustPick ,0) as MustPick ,ISNULL(Lead ,0) as Lead                                                                                                      
   ,ISNULL(NonLead,0) as NonLead , convert(char(10),CreationDate,101) as CreationDate,ISNULL(TBW,0) as TBW,                                        
   SortOrder,NLDate,[Day], PostURL, Direction  , ISNULL(IsArticleLevelPost,0) as IsArticleLevelPost,Sources, HeadLine ,          
    ISNULL(PromoteTo,0) as PromoteTo,ParentID, PostLevel, IsHLTestPassed, Synapsis, ProjectedViews,ISNULL([SaveChecked],0) as [SaveChecked]                       
 FROM  PromoteToArticleNLPrep                                                                                                                                                             
where day='Wednesday' and UserName = @UserName <--Condition [where day='Wednesday']                                                                                                                               
order by ISNULL(SortOrder,0) asc
.....
.....
.....
..... and so on for rest of the days...
END

Note - day is a column name

So, in all, there will be 7 tables returning for each day. For this i would like to use SELECT Query at the minimal level, how should i do that..

Also I have 2 below options to remove redundancy -

  1. Use While loop 7 times where day will be dyanamic
  2. Static SQL Query N'' only day will be dyanmic

Expected- A single Select query which will return 7 tables for each day, instead of using 7 different select statements.

Any other suggestions or thoughts on this..?

  • since i am not sure what exactly you try to achieve i think you can add another column with day, and return a single table with day to differentiate the records returned, – Roy Feb 11 '16 at 09:59
  • How do you continue with these result sets? If you have just one SELECT and you include "day" in the column's list and sort by `day, SortOrder` (btw. read about "sargable" and `ISNULL()`!) you get the same in one big list... – Shnugo Feb 11 '16 at 10:01
  • And btw: Both options you state (loop and dynamic SQL) are (in most cases) a NoGo... – Shnugo Feb 11 '16 at 10:02
  • @Shnugo but, i want to return the 7 tables from SP –  Feb 11 '16 at 10:03
  • What are you going to do with them? If you call a SP from code you will get just the last SELECT. I'll try an answer soon... – Shnugo Feb 11 '16 at 10:05
  • What you write as "expected" result does not exist... One single SELECT will come back with one single result set. You might add grouped counters (look at `ROW_NUMBER() OVER(PARTITION BY day ORDER BY ...`). You might store the results within 7 physically created tables... But this is really - uhm - ugly... – Shnugo Feb 11 '16 at 10:17

1 Answers1

0

Stored Procedures are methods and meant to do something. Functions are meant to read and deliver...

This is my suggestion:

I did not change anything in your query, just added the day column... There's quite some chance to improve...

CREATE FUNCTION dbo.GetFilteredArticles
(
    @UserName VARCHAR(150)=NULL
   ,@WeekDay VARCHAR(30)=NULL
)
RETURNS TABLE
AS
RETURN
SELECT day, PostID,Title,convert(char(10),PostDate,101) as PostDate,IsEntertainment, ISNULL(DrPick ,0) as DrPick,                                               
   ImageURL,ISNULL(MustPick ,0) as MustPick ,ISNULL(Lead ,0) as Lead                                                                                                 
   ,ISNULL(NonLead,0) as NonLead , convert(char(10),CreationDate,101) as CreationDate,ISNULL(TBW,0) as TBW,                                        
   SortOrder,NLDate,[Day], PostURL, Direction,ISNULL(IsArticleLevelPost,0) as IsArticleLevelPost ,Sources, HeadLine,                                   
   ISNULL(PromoteTo,0) as PromoteTo,ParentID, PostLevel, IsHLTestPassed, Synapsis, ProjectedViews,ISNULL([SaveChecked],0) as [SaveChecked]                                     
   FROM  PromoteToArticleNLPrep                                      
where @WeekDay IS NULL OR day=@WeekDay 
  and UserName = @UserName;                                                                                                                  

GO

--You might call this like this: You'll get all rows sorted 
SELECT * FROM dbo.GetFilteredArticles(NULL,'Pranav')
ORDER BY day, CASE WHEN SortOrder IS NULL THEN 0 ELSE SortOrder END asc

--or for one day
SELECT * FROM dbo.GetFilteredArticles('Tuesday','Pranav')
ORDER BY day, CASE WHEN SortOrder IS NULL THEN 0 ELSE SortOrder END asc
Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • I appreciate what you did, but still I need to write 'Select' statement for all days in procedure + the function...right? –  Feb 11 '16 at 10:18
  • You will **only** get "7 tables" when you call this SP in SSMS and do nothing else. For this you do not need any SP... Just write your calls directly into your query window and save this. This is *kind of* a SP... I think you really need some basic learning about row-based thinking. What's wrong with a sorted call with `@WeekDay=NULL`? – Shnugo Feb 11 '16 at 10:27
  • Thanks...!! will try and let you know –  Feb 11 '16 at 10:33
  • @PranavBilurkar, Did this work for you? Do you need further help? – Shnugo Feb 12 '16 at 10:32
  • @PranavBilurkar, if this question is solved it would be very kind to use your right to vote on contributions. This is the fuel SO runs with. And please tick the acceptance check below the vote counter if my answer helped you out. This will show to other users, that this question is solved. If you need further help please ask a new question and place a link here as a comment. These two question will show up as "linked questions" then. – Shnugo Feb 18 '16 at 08:03