Click here to Skip to main content
15,902,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
USE [HIMT_Testing]
GO
/****** Object:  StoredProcedure [dbo].[OH_Course_Elg]    Script Date: 03/10/2015 15:51:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

 ALTER Procedure [dbo].[OH_Course_Elg] @Studid varchar(100)
 as
 begin

 declare  @Coursename varchar(100),
          @code varchar(100),
          @certificate varchar(100),
          @issueauth varchar(100),
          @issuedate varchar(100),@Cousrseelg Varchar(100)
           
 create table #Temptable (Coursename varchar(100))

 create table #Temptable1 (code varchar(100),
                          certificate varchar(100),issuseauth varchar(100),issuedate varchar(100))
 begin tran


 declare coursename cursor for
   select distinct Course = case cr.cpm_pkg_id WHEN '' THEN cr.cmn_minor_code else  cbm.cmn_minor_code end 
   from course_registration cr, batch_course_registration bcr, co_batch_master cbm where bcr.cr_bill_no 
   = cr.cr_bill_no and cbm.cbm_batch_id = bcr.bcr_batch_id and cr.cr_active = 'A' --and cbm.cbm_batch_start_dt > getdate()
   and datepart(year,cbm.cbm_batch_start_dt) <> '3000' and datepart(year,cbm.cbm_batch_start_dt) <> '4000' 
   and datepart(year,cbm.cbm_batch_start_dt) <> '5000' and datepart(year,cbm.cbm_batch_start_dt) <> '6000' 
   and cr.stud_id = @Studid

 open coursename
  fetch next from coursename into @Coursename
  while @@Fetch_status = 0
     begin   
       begin tran
         declare crselg cursor for
            select distinct a.pm_prof_code as Code,a.sp_cert_no as Certificate_No,a.sp_issu_authority as Issue_Authority, convert(char(14),a.sp_issu_dt,106) as Issue_Date,
             b.Courseelg as Cousrseelg from student_professional a,tb_courseelg_settings b where a.pm_prof_code= b.courseelg and b.coursename = @Coursename
 and a.stud_id = @studid order by Issue_Date desc 

--print 'inside'
         open crselg 
            fetch next from crselg into @code,@certificate,@issueauth,@issuedate,@Cousrseelg
             while @@Fetch_status = 0
			 begin    
                 insert into #Temptable values(@Coursename)
                 insert into #Temptable1 values (@code,@certificate,@issueauth,@issuedate)
             fetch next from crselg into @code,@certificate,@issueauth,@issuedate,@Cousrseelg
             end
        close crselg
        deallocate crselg
        commit tran
    fetch next from coursename into @Coursename
    end
 close coursename
 deallocate coursename
 commit tran
select * from #Temptable
end
select * from #Temptable1
end


When i run the above store procedure shows error as follows
Incorrect syntax near 'end'.

What is the problem in my store procedure.

Regards,
Narasiman P.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 12-Mar-15 0:33am
v2
Comments
Herman<T>.Instance 12-Mar-15 6:36am    
no line nr given with the error?

Hi,

Check this below code once.

SQL
Create Procedure [dbo].[OH_Course_Elg] @Studid varchar(100)
 as
 begin 
 declare  @Coursename varchar(100),
          @code varchar(100),
          @certificate varchar(100),
          @issueauth varchar(100),
          @issuedate varchar(100),@Cousrseelg Varchar(100)
           
 create table #Temptable (Coursename varchar(100))
 
 create table #Temptable1 (code varchar(100),
                          certificate varchar(100),issuseauth varchar(100),issuedate varchar(100))
 begin tran
 declare coursename cursor for
   select distinct Course = case cr.cpm_pkg_id WHEN '' THEN cr.cmn_minor_code else  cbm.cmn_minor_code end 
   from course_registration cr, batch_course_registration bcr, co_batch_master cbm where bcr.cr_bill_no 
   = cr.cr_bill_no and cbm.cbm_batch_id = bcr.bcr_batch_id and cr.cr_active = 'A' --and cbm.cbm_batch_start_dt > getdate()
   and datepart(year,cbm.cbm_batch_start_dt) <> '3000' and datepart(year,cbm.cbm_batch_start_dt) <> '4000' 
   and datepart(year,cbm.cbm_batch_start_dt) <> '5000' and datepart(year,cbm.cbm_batch_start_dt) <> '6000' 
   and cr.stud_id = @Studid 
 open coursename
  fetch next from coursename into @Coursename
  while @@Fetch_status = 0
     begin   
       begin tran
         declare crselg cursor for
            select distinct a.pm_prof_code as Code,a.sp_cert_no as Certificate_No,a.sp_issu_authority as Issue_Authority, convert(char(14),a.sp_issu_dt,106) as Issue_Date,
             b.Courseelg as Cousrseelg from student_professional a,tb_courseelg_settings b where a.pm_prof_code= b.courseelg and b.coursename = @Coursename
 and a.stud_id = @studid order by Issue_Date desc 
 
--print 'inside'
         open crselg 
            fetch next from crselg into @code,@certificate,@issueauth,@issuedate,@Cousrseelg
             while @@Fetch_status = 0
			 begin    
                 insert into #Temptable values(@Coursename)
                 insert into #Temptable1 values (@code,@certificate,@issueauth,@issuedate)
             fetch next from crselg into @code,@certificate,@issueauth,@issuedate,@Cousrseelg
             end
        close crselg
        deallocate crselg
        commit tran
    fetch next from coursename into @Coursename
    end
 close coursename
 deallocate coursename
 commit tran
select * from #Temptable
select * from #Temptable1
end

Your statements:
SQL
select * from #Temptable
end (at here the begin will be closed so that's why it's giving error for next end).
select * from #Temptable1
end
 
Share this answer
 
v2
Hello,

Here is refined stored procedure with no error..

USE [HIMT_Testing]
GO
/****** Object: StoredProcedure [dbo].[OH_Course_Elg] Script Date: 03/10/2015 15:51:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create Procedure [dbo].[OH_Course_Elg] @Studid varchar(100)
as
begin

declare @Coursename varchar(100),
@code varchar(100),
@certificate varchar(100),
@issueauth varchar(100),
@issuedate varchar(100),@Cousrseelg Varchar(100)

create table #Temptable (Coursename varchar(100))

create table #Temptable1 (code varchar(100),
certificate varchar(100),issuseauth varchar(100),issuedate varchar(100))
begin tran


declare coursename cursor for
select distinct Course = case cr.cpm_pkg_id WHEN '' THEN cr.cmn_minor_code else cbm.cmn_minor_code end
from course_registration cr, batch_course_registration bcr, co_batch_master cbm where bcr.cr_bill_no
= cr.cr_bill_no and cbm.cbm_batch_id = bcr.bcr_batch_id and cr.cr_active = 'A' --and cbm.cbm_batch_start_dt > getdate()
and datepart(year,cbm.cbm_batch_start_dt) <> '3000' and datepart(year,cbm.cbm_batch_start_dt) <> '4000'
and datepart(year,cbm.cbm_batch_start_dt) <> '5000' and datepart(year,cbm.cbm_batch_start_dt) <> '6000'
and cr.stud_id = @Studid

open coursename
fetch next from coursename into @Coursename
while @@Fetch_status = 0
begin
begin tran
declare crselg cursor for
select distinct a.pm_prof_code as Code,a.sp_cert_no as Certificate_No,a.sp_issu_authority as Issue_Authority, convert(char(14),a.sp_issu_dt,106) as Issue_Date,
b.Courseelg as Cousrseelg from student_professional a,tb_courseelg_settings b where a.pm_prof_code= b.courseelg and b.coursename = @Coursename
and a.stud_id = @studid order by Issue_Date desc

--print 'inside'
open crselg
fetch next from crselg into @code,@certificate,@issueauth,@issuedate,@Cousrseelg
while @@Fetch_status = 0
begin
insert into #Temptable values(@Coursename)
insert into #Temptable1 values (@code,@certificate,@issueauth,@issuedate)
fetch next from crselg into @code,@certificate,@issueauth,@issuedate,@Cousrseelg
end
close crselg
deallocate crselg
commit tran
fetch next from coursename into @Coursename
End
close coursename
deallocate coursename
commit tran

End

select * from #Temptable

select * from #Temptable1

-----------the problem was with this statements:

select * from #Temptable
end
select * from #Temptable1
end

If you want to put 'end' after select statement you have to again write 'Begin' For 'End' statement i.e

Begin
select * from #Temptable
end

Begin
select * from #Temptable1
end
 
Share this answer
 

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