Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Plz solve this problem

SQL
alter PROCEDURE [dbo].[sp_ShowTimetable]   
(
 @date datetime
)
as
begin


DECLARE @cols AS NVARCHAR(MAX), @query  AS NVARCHAR(MAX);


SELECT @cols= ISNULL(@cols + ',','') + QUOTENAME(Period_Name)
  FROM (Select Period_Name from tbl_TimetablePeriod where Active=1) AS x;

 select @cols

SET @query= 'Select Location_Name,'+@cols+'
from
(
 Select  temp2.Course_Name+'', '' + temp2.CLASSNAME+'', ''+temp2.TEACHER as TimeTableClass, temp2.Period_Name,TL.Location_Name  from

  (Select  TT.*,TC.Course_Name,TB.Name AS CLASSNAME,TE.First_Name +'' ''+ ISNULL(TE.Last_Name,'''') AS TEACHER,TP.Period_Name
   from tbl_timetable TT JOIN tbl_Course TC ON TT.CourseId=TC.Course_Id
   JOIN tbl_Batch TB on TT.ClassId=TB.Batch_Id
   JOIN tbl_Employees TE on TT.empid=TE.empid
   JOIN tbl_Timetableperiod TP on TT.periodid=TP.Period_Id Where TT.date= convert(datetime,''+@date+'',121))


 as temp2 Right Outer Join  tbl_timetablelocation TL ON temp2.Loactionid =TL.Location_Id
) src
pivot
(
  max(TimeTableClass)
  for Period_Name in ('+@cols+')
) piv'

Exec(@query)

select @query


End




execute :- [sp_ShowTimetable] '04/06/2015'
Posted
Updated 9-Jun-15 21:56pm
v2
Comments
CHill60 10-Jun-15 4:05am    
Try passing the date in unambiguously as '04-JUN-2015' (or '06-APR-2015').
If that still fails try running the query in SSMS/Query Analyser and give the full error
Abhipal Singh 10-Jun-15 4:11am    
Chill60 is right, try that and you will figure out why it is not working.

Also, a word of advice, dynamic queries are performance killers and error prone. Try to avoid them if possible.

1 solution

You do here something very strange! @DATE IS a datetime so why to turn it into string and try to parse it back to datetime???
I believe that the origin for that confusion is that you do not understand that datetime is a BINARY format that neglect any formating and the display format you see in SSMS is - well a display format only...
All you need is drop that unnecessary converting and compare date with date...on binary base...
SQL
WHERE TT.DATE = @DATE
 
Share this answer
 
Comments
ssss0001 10-Jun-15 5:52am    
I have try this code WHERE TT.DATE = @DATE

but the error message is :-

(1 row(s) affected)
Msg 137, Level 15, State 2, Line 10
Must declare the scalar variable "@date".

(1 row(s) affected)
Kornfeld Eliyahu Peter 10-Jun-15 5:55am    
You have to adjust my solution to the fact, that you are using dynamic query...

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