Take the 2-minute tour ×
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.
ALTER  Procedure [dbo].[HRptForm3A]
@StrCond Varchar(1000),
@SelYear int,
@Type varchar(1)
as
Declare @Tbl varchar(30)
    if ( @Type = 'O')
        begin
            set @Tbl = 'TblResult'
        end
    else if( @Type = 'I')
        begin
            set @Tbl = 'TblGResult'
        end
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,
Case 
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
From 
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(@Qry2)


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

GO

when i change the highlighted value Month(P.FromDate) to Month(P.FromDate)+1. its showing exception error in the coding part of vb.net 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 
    Try
      While IdOfEmp = Val(DS.Tables(0).Rows(TotalCnt).Item("EmpId")) 
        If J = Val(DS.Tables(0).Rows(TotalCnt).Item("MonthNumber")) 
          Then .PrintString(" | | | | | | | | |") : 
               .NewLine() : 
               .Tab(.LeftMargin)
        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

1 Answer 1

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:

Case 
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

 
discard

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.