Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
ALTER  Procedure [dbo].[HRptForm3A]
@StrCond Varchar(1000),
@SelYear int,
@Type varchar(1)
Declare @Tbl varchar(30)
    if ( @Type = 'O')
            set @Tbl = 'TblResult'
    else if( @Type = 'I')
            set @Tbl = 'TblGResult'
Declare @FromPayPeriodId Int
Declare @ToPayPeriodId Int
    select @FromPayPeriodId=isnull(Min(PayPeriodId),0),@ToPayPeriodId=isnull(Max(PayPeriodId),0) from tblPayPeriodMst Where 
    Month(FromDate) in (4,5,6,7,8,9,10,11,12) and Year(FromDate)=@SelYear or
    Month(FromDate) in (1,2,3)and Year(FromDate)=@SelYear+1
Declare @Qry2 Varchar(1000)
Set @Qry2='Select E.Empid,E.Empcode,E.Empname,E.EmpFhName,E.Condate,E.PfNo as AccountNo,E.VPF,E.Leftdate,E.ReasonForLeft,R.payperiodId,
R.Lop,R.LWF,R.PFGross,R.PF,R.EPFE,R.EPFR, **Month(P.FromDate)+1** as Monthnumber ,Year(P.FromDate) as YearNumber,
When Month(P.FromDate) in(4,5,6,7,8,9,10,11,12) then  Month(P.FromDate)
when Month(P.FromDate) in(1,2,3) then  Month(P.FromDate)
end as Mnth
PayEmpMstView as E,' + @Tbl + ' as R,tblPayPeriodMst as P 
where  E.Empid=R.Empid  and len(E.PFNo)>0
and R.PayPeriodId=P.PayPeriodid  and 
P.PayPeriodid between ' + Convert(Varchar(10),@FromPayPeriodId) + ' and '+ Convert(Varchar(10),@ToPayPeriodId)+ @StrCond +
' Order by E.EmpCode,Year(P.FromDate),Month(P.ToDate)'

Exec ('Select E.EmpCode,E.EmpId  from tblPayEmployee E Where  0=0  ' +  @StrCond )


when i change the highlighted value Month(P.FromDate) to Month(P.FromDate)+1. its showing exception error in the coding part of as HRESULT:800340 error this change is working in sql server 2000 but not in 2005. how to overcome this

Karthik added this code in the comments. It appears to be VB.NET code. It's not super legible.

MNth(4) = "Apr-" & Year : 
MNth(5) = "May-" & Year.ToString : 
MNth(6) = "Jun-" & Year.ToString : 
MNth(7) = "Jul-" & Year.ToString 
MNth(8) = "Aug-" & Year : 
MNth(9) = "Sep-" & Year.ToString : 
MNth(10) = "Oct-" & Year.ToString : 
MNth(11) = "Nov-" & Year.ToString 
MNth(0) = "Dec-" & Year : 
MNth(1) = "Jan-" & (Year + 1).ToString : 
MNth(2) = "Feb-" & (Year + 1).ToString : 
MNth(3) = "Mar-" & (Year + 1).ToString
Dim J As Integer J = 4 ' k = 1 
  IdOfEmp = Val(DS.Tables(0).Rows(TotalCnt).Item("EmpId")) 
  While J < 13 
      While IdOfEmp = Val(DS.Tables(0).Rows(TotalCnt).Item("EmpId")) 
        If J = Val(DS.Tables(0).Rows(TotalCnt).Item("MonthNumber")) 
          Then .PrintString(" | | | | | | | | |") : 
               .NewLine() : 
        If J = 12 
          Then J = 0 
        End If 
       .PrintString(MNth(J), 9, DosPrint.Align.Left)

I'm not sure that the best approaches are being followed here, but ... I'm just trying to clean things up.

share|improve this question

So many problems with this code. The error is right here:

.PrintString(MNth(J), 9, DosPrint.Align.Left)

Since J is a number between 2 and 13 when it returns from sql and then in the set 0,2..11,13 after the if statement, when it is 13 MNth(13) will throw an array out of bounds.

The fix is in - add the following line (if statement for context.)

 J = J - 1  ' now J goes from 1-12
 If J = 12 
      Then J = 0 
 End If 

Another note (I can't resist pointing this one out but there are many...)

This part of your query:

When Month(P.FromDate) in(4,5,6,7,8,9,10,11,12) then  Month(P.FromDate)
when Month(P.FromDate) in(1,2,3) then  Month(P.FromDate)
end as Mnth

could be written as:

Month(P.FromDate) as Mnth

Since Month() is defined to return a value from 1..12

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.