Hello Everyone,I have trying to create a stored procedure. Please see the code below. But I cannot get away from two errors. Incorrect Syntax near CLOSE. Expecting CONVERSATION - at the bottom when I try to close the cursor&Incorrect object name '#tmp_hotel_dash' -- in the update statement towards the bottom when I try to update the temp table with the values calculated by the cursor. CREATE PROCEDURE sphotel_DashAS IF OBJECT_ID('tempdb..#tmp_hotel_dash') IS NOT NULL DROP TABLE #tmp_hotel_dash;create table #tmp_hotel_dash (rownum int,opr_date smalldatetime,opr_num varchar(50),room varchar(50),site varchar(50),house_maid varchar(100),opr_service varchar(50),orig_sch_start smalldatetime,opr_start smalldatetime,opr_start_min int,start_day varchar(25),opr_stop smalldatetime,opr_stop_min int,hotel_loc varchar(50),prev_stop_min int)insert into #tmp_hotel_dash (rownum, opr_date, opr_num, room, site, house_maid,opr_service,orig_sch_start, opr_start, opr_start_min, start_day, opr_stop, opr_stop_min, hotel_loc, prev_stop_min)select row_number() over(order by site, room, CAST(opr_start_tm as datetime), house_maid)as rownum, opr_date, opr_num, room, site, house_maid, opr_service, CAST(orig_sch_start_tm as smalldatetime) as orig_sch_start, CAST(opr_start_tm as smalldatetime) as opr_start, DATEPART(HH,CAST(opr_start_tm as smalldatetime))*60+DATEPART(MI,CAST(opr_start_tm as datetime)) as opr_start_min,DATENAME(DW,opr_date) as start_day, CAST(opr_stop_tm as smalldatetime) as opr_stop,DATEPART(HH,CAST(opr_stop_tm as smalldatetime))*60+DATEPART(MI,CAST(opr_stop_tm as datetime)) as opr_stop_min,hotel_loc,NULLfrom [MyDB].[MySchm].[thotel_Dash_Data]order by 5,4,9,6GO--select * from #tmp_hotel_dash;declare @prev_stop_min int;declare @rownum intdeclare @room varchar(50)declare @maid varchar(100)declare @opr_start_min as intdeclare @start_day varchar(25)declare @opr_stop_min intdeclare @rownum2 intdeclare @room2 varchar(50)declare @maid2 varchar(100)declare @opr_start_min2 as intdeclare @start_day2 varchar(25)declare @opr_stop_min2 intdeclare hotel_cur cursor forselect rownum, room, house_maid, opr_start_min,start_day, opr_stop_minfrom #tmp_hotel_dashorder by rownumOPEN hotel_curFETCH FIRST from hotel_cur into @rownum, @room, @maid, @opr_start_min, @start_day, @opr_stop_minFETCH NEXT from hotel_cur into @rownum2, @room2, @maid2, @opr_start_min2, @start_day2, @opr_stop_min2WHILE @@FETCH_STATUS = 0BEGIN SET @prev_stop_min = 0 IF @room2 = @room AND @maid2 = @maid BEGIN SET @prev_stop_min = @opr_stop_min END END UPDATE #tmp_hotel_dash <<<<----------- Error 2 SET prev_stop_min = @prev_stop_min WHERE rownum = @rownumEND CLOSE hotel_cur <<<------- Error 1. DEALLOCATE hotel_cur
Any insights and recommendations greatly appreciated.Thanks