title: How to insert value to table using cursor in stored procedure without getting duplicate records?
body: <p>I am trying to insert records into existing but empty table based on from date and to date parameters using cursor in stored procedure. Please let me know what I am doing wrong in the below SQL? </p>
  
  <p>When executing this procedure I am getting first row duplicated multiple times.</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Maximum stored procedure, function, trigger or view nesting level exceeded (limit32)</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>ALTER proc [dbo].[spempmaster] (@date1 datetime,@date2 datetime)
  as
  Begin
  
      Set nocount on
  
      declare @doj datetime
      declare @empname nchar(10)
      declare @managername nchar(10)
      declare @dept varchar(50)  
      declare emp_report15 cursor for
  
      select convert(varchar(10),convert(smalldatetime,emp.doj,120),103) DOJ, 
      (emp.name + ' ' + emp.lastname)  Name,
      emp1.name Manager_Name, txtdepartment Department
      from empmaster emp
      left outer join tbljobtitles jt 
          on emp.fkjobtitleid = jt.pkjobtitleid,
      tbldepartment td, 
      tblteam t,
      empmaster emp1
      where
          jt.fkteamid = t.pkteamid
          and td.pkdeptid= t.fkdeptid
          and emp.reportingto = emp1.empno
          and emp.doj between @date1 and @date2
      order by doj
  
      open emp_report15
  
      fetch emp_report15 into @doj, @empname, @managername, @dept
  
      while @@fetch_status = 0
      begin
          insert into tblreport (DOJ,emp_name,manager_name,department)
          values(@doj,@empname,@managername,@dept)
      end
  
      fetch next from emp_report15 into @doj,@empname,@managername,@dept
  
      close emp_report15
  
      deallocate emp_report15
  
  end
  </code></pre>