Question : Arithmetic overflow error converting int to data type numeric. SQL 2005

Please note the SQL is handled dynamically by the SQL Server, therefore some items in my WHERE clauses will look odd to you PLEASE ignore this as its not an issue. I am getting the following error in my report and need some help.

Msg 8115, Level 16, State 8, Line 100
Arithmetic overflow error converting int to data type numeric.
The statement has been terminated.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
/* Billing Status Report 11/22/03  
Revised 5/6/06 */
SET NOCOUNT ON

CREATE TABLE #Records
    (
      TicketNumber varchar(20),
      Visit datetime,
      LastDateFiled datetime,
      FirstDateFiled datetime,
      InsBalance money,
      PatBalance money,
      PrimaryInsuranceCarrier varchar(50),
      CurrentInsuranceCarrier varchar(50),
      CurrentCarrier numeric(1, 0),
      BillStatus varchar(50),
      BillStatusID int,
      Entered datetime,
      FilingType int,
      DaysSinceFiled int,
      PatientName varchar(50),
      VisitOwner varchar(50),
      Description varchar(100),
      Doctor varchar(100),
      Credentialed varchar(1)
    )

-- Insert the New Records
IF '1' = '1'
    OR 1 IN ( NULL ) 
    INSERT  INTO #Records
            SELECT  ISNULL(a.TicketNumber, 'None'),
                    CONVERT(varchar, a.ApptStart, 101),
                    '',
                    '',
                    0,
                    0,
                    ISNULL(ic.ListName, 'None'),
                    ISNULL(ic.ListName, 'None'),
                    1,
                    'New',
                    1,
                    '',
                    0,
                    0,
                    pp.Last + ', ' + pp.First + ' - ' + pp.PatientID,
                    'Appointment',
                    '',
                    d.ListName,
                    CASE WHEN icd.InsuranceCarriersDoctorID IS NOT NULL
                         THEN 'X'
                         ELSE ' '
                    END
            FROM    dbo.InsuranceCarriers ic
                    INNER JOIN dbo.PatientInsurance pi ON ic.InsuranceCarriersId = pi.InsuranceCarriersId
                    RIGHT OUTER JOIN dbo.Appointments a ON pi.PatientProfileId = a.OwnerId
                    INNER JOIN dbo.PatientProfile pp ON a.OwnerID = pp.PatientProfileID
                    INNER JOIN DoctorFacility d ON ISNULL(a.DoctorID,
                                                          a.ResourceID) = d.DoctorFacilityID
                    LEFT JOIN InsuranceCarriersDoctor icd ON pi.InsuranceCarriersID = icd.InsuranceCarriersID
                                                             AND ISNULL(a.DoctorID, a.ResourceID) = icd.DoctorID
            WHERE   ( pi.OrderForClaims = 1 )
                    AND ( a.PatientVisitId IS NULL )
                    AND ( a.ApptKind = 1 )
                    AND ( a.HideNewVisit IS NULL
                          OR a.HideNewVisit = 0
                        )
                    AND ( a.Canceled IS NULL
                          OR a.Canceled = 0
                        )
                    AND ( a.ApptStart < GETDATE() )
                    AND a.ApptStart >= ISNULL(NULL, '1/1/1900')
                    AND a.ApptStart < dateadd(day, 1, ISNULL(NULL, '1/1/3000'))
                    AND  --Filter on insurance carrier
                    ( ( NULL IS NOT NULL
                        AND ic.InsuranceCarriersId IN ( NULL )
                      )
                      OR ( NULL IS NULL )
                    )
                    AND  --Filter on facility
                    ( ( NULL IS NOT NULL
                        AND a.FacilityID IN ( NULL )
                      )
                      OR ( NULL IS NULL )
                    )
                    AND  --Filter on insurance group
                    ( ( NULL IS NOT NULL
                        AND ic.InsuranceGroupId IN ( NULL )
                      )
                      OR ( NULL IS NULL )
                    )
                    AND  --Filter on Doctor
                    ( ( NULL IS NOT NULL
                        AND a.ResourceID IN ( NULL )
                      )
                      OR ( NULL IS NULL )
                    )
                
-- Enter the Visit Records
INSERT  INTO #Records
        SELECT  pv.TicketNumber,
                pv.Visit,
                pv.LastFiledDate,
                pv.FirstFiledDate,
                pva.InsBalance,
                pva.PatBalance,
                ISNULL(ic.ListName, 'None'),
                ISNULL(ic.ListName, 'None'),
                pv.CurrentCarrier,
                bs.Description,
                pv.BillStatus,
                pv.Entered,
                ISNULL(pv.FilingType, 0),
                CASE WHEN pv.LastFiledDate IS NULL THEN 0
                     ELSE DATEDIFF(d, pv.LastFiledDate, getdate())
                END,
                pp.Last + ', ' + pp.First + ' - ' + pp.PatientID,
                ISNULL(vo.Description, 'No Owner'),
                ISNULL(LEFT(pv.Description, 100), ''),
                d.ListName,
                CASE WHEN icd.InsuranceCarriersDoctorID IS NOT NULL THEN 'X'
                     ELSE ' '
                END
        FROM    dbo.PatientVisit pv
                INNER JOIN dbo.PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId
                LEFT OUTER JOIN dbo.InsuranceCarriers ic ON pv.PrimaryInsuranceCarriersId = ic.InsuranceCarriersId
                LEFT OUTER JOIN dbo.PatientVisitAgg pva ON pv.PatientVisitId = pva.PatientVisitId
                LEFT OUTER JOIN dbo.InsuranceCarriers cic ON pv.CurrentInsuranceCarriersId = cic.InsuranceCarriersId
                LEFT OUTER JOIN dbo.MedLists bs ON bs.JoinId = pv.BillStatus
                                                   AND bs.TableName = 'BillStatus'
                INNER JOIN DoctorFacility d ON pv.DoctorID = d.DoctorFacilityID
                LEFT OUTER JOIN MedLists vo ON pv.VisitOwnerMID = vo.MedListsID
                LEFT JOIN InsuranceCarriersDoctor icd ON pv.CurrentInsuranceCarriersId = icd.InsuranceCarriersID
                                                         AND pv.DoctorID = icd.DoctorID
        WHERE   pv.Visit >= ISNULL(NULL, '1/1/1900')
                AND pv.Visit < dateadd(day, 1, ISNULL(NULL, '1/1/3000'))
                AND pv.BillStatus NOT IN ( 12, 10 )
	--Filter on BillStatus
                AND ( ( NULL IS NULL
                        AND '1' = '1'
                      )
                      OR ( '1' = '2'
                           AND NULL IS NULL
                         )
                      OR pv.BillStatus IN ( NULL )
                    )
                AND  --Filter on insurance carrier
                ( ( NULL IS NOT NULL
                    AND ic.InsuranceCarriersId IN ( NULL )
                  )
                  OR ( NULL IS NULL )
                )
                AND  --Filter on facility
                ( ( NULL IS NOT NULL
                    AND pv.FacilityID IN ( NULL )
                  )
                  OR ( NULL IS NULL )
                )
                AND  --Filter on insurance group
                ( ( NULL IS NOT NULL
                    AND ic.InsuranceGroupId IN ( NULL )
                  )
                  OR ( NULL IS NULL )
                )
                AND -- Carrier priority to include
                ( ( 1 = 1 )
                  OR ( 1 = 2
                       AND pv.CurrentCarrier = 1
                     )
                  OR ( 1 = 3
                       AND pv.CurrentCarrier > 1
                     )
                )
                AND  --Filter on visitowner
                ( ( NULL IS NOT NULL
                    AND pv.VisitOwnerMID IN ( NULL )
                  )
                  OR ( NULL IS NULL )
                )
                AND  --Filter on Doctor
                ( ( NULL IS NOT NULL
                    AND pv.DoctorID IN ( NULL )
                  )
                  OR ( NULL IS NULL )
                )

IF 0 <> 1 
    SELECT  *
    FROM    #Records
ELSE 
    SELECT  *
    FROM    #Records
    WHERE   BillStatus <> 'New'

Answer : Arithmetic overflow error converting int to data type numeric. SQL 2005

See Restoring a Complete Backup to a New Database on the Same Server

http://technet.microsoft.com/en-us/library/cc966495.aspx#E5AA
Random Solutions  
 
programming4us programming4us