Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi,
i want to create a stroed procedure for display 4 week records after insert date
and i wrote this stored procedure
but i got an error
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'group'.

and i think this error occured after second while loop.
please check my S.P and give me a solution for this

SQL
--exec [AA_SP_Weekly] '2010-08-28'

ALTER procedure [dbo].[AA_SP_Weekly]
 @startingDate datetime
as
begin

  declare @numWeeks int
  declare @selectClause varchar(8000)
  declare @fromClause varchar(8000)
  declare @whereClause varchar(8000)
  declare @groupByClause varchar(8000)
  declare @i int;
  declare @sDate datetime
  declare @colHead varchar(255)
  declare @case varchar(1000)
  declare @cases varchar(8000)
  declare @sqlInner varchar(8000)

  set @numWeeks = 4  
  set @i = 0;
  set @cases = ''
  
-- while loop start
  while (@i < @numWeeks)
  begin
    set @sDate = @startingDate + (@i * 7)
    set @colHead = '[Week' + CONVERT(varchar(2), @i) + ']'
set @case = ', case when StartDate >= ''' + CONVERT(varchar(30),@sDate,101) + '''and StartDate < ''' + CONVERT(varchar(30), @sDate + 7, 101) + ''' then COUNT(CourseName) else 0 end'
    
    set @cases = @cases + '' +@case + ' as ' + @colHead
    set @i = @i + 1
  end  
-- while loop end
  
  set @selectClause = 'select CourseName' + @cases   
  set @fromClause = 'from GL_BookingList bl inner join GL_MasterPriceDefaultSelection mpds on bl.PriceItemId=mpds.PriceItemId inner join GL_Course on GL_Course.CourseID=mpds.SelectionId'
 
 set @whereClause = ' where (GL_Course.StartDate >= '''+ CONVERT(varchar(30),@startingDate, 101) + ''' and GL_Course.StartDate < ''' + CONVERT(varchar(30), @startingDate + (@numWeeks * 7), 101) + ''')'
 
  set @sqlInner = @selectClause + @fromClause + @whereClause
  
     --print @sqlInner

  set @i = 0;
  set @cases = ''
  
-- while loop start
  while (@i < @numWeeks)
  begin
    set @sDate = @startingDate + (@i * 7)
    set @colHead = '[Week of ' + CONVERT(varchar(255), @sDate, 6) + ']'
    set @case = ', Sum([Week' + CONVERT(varchar(2), @i) + '])'
    
    set @cases = @cases + '' +@case + ' as ' + @colHead
    set @i = @i + 1
  end  
-- while loop end

  set @selectClause = 'select CourseName' + @cases
  set @fromClause = '  from (' + @sqlInner + ')'
  set @groupByClause = ' group by CourseName,GL_Course.StartDate'  

	print (@selectClause + @fromClause + @groupByClause)
	execute(@selectClause + @fromClause + @groupByClause)
end
--exec [AA_SP_Weekly] '2010-08-20'
thanks in advance
Posted
Updated 2-Nov-14 21:56pm
v2
Comments
Maciej Los 3-Nov-14 3:58am    
Have you tested what print method returns?
Kornfeld Eliyahu Peter 3-Nov-14 3:58am    
See the final SQL statement you get when printing it! If you can't see what the problem, share it with us!

1 solution

In the query you produce you need to name the inner query like this:
SQL
SELECT ...
FROM (
	...
	) x -- name it yourself
GROUP BY CourseName,GL_Course.StartDate


The inner query needs to have an alias even if you don't reference it explicitly in the outer query.

Edit

In your code you need to change this:

SQL
set @fromClause = '  from (' + @sqlInner + ') x'
 
Share this answer
 
v2
Comments
Manish Dalwadi 3-Nov-14 4:14am    
thanx dr. for give me your time and your response
i did it
thank you so much again for response me
Maciej Los 3-Nov-14 4:27am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900