Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
simondeutsch
Aged Yak Warrior
547 Posts |
Posted - 2003-06-19 : 12:17:51
|
I'm getting the "Internal SQL Server Error" message when trying to run a query. I guess this is the servers way of saying "Help! I can't handle this!", because this message is way vague. I have isolated the error to this part of the query:SELECT OD.PaySource, (CASE WHEN FromMonth < 10 THEN CAST(FromMonth AS CHAR(1)) + 'm' ELSE (CASE WHEN FromMonth > 9 AND FromMonth < 12 THEN CAST(FromMonth AS CHAR(2)) + 'm' ELSE RTRIM(CAST(FromMonth / 12 AS CHAR(3))) + 'y' END) END) + '-' + (CASE WHEN ToMonth < 12 THEN RTRIM(CAST(ToMonth AS CHAR(2))) + 'm' ELSE RTRIM(CAST(ToMonth / 12 AS CHAR(3))) + 'y' END) + ' ' + Sex AS Cat, Pts,Visits,ISNULL(FFS,0) AS FFS,((Amount / PtCount) * Pts) + ISNULL(FFS,0) AS CapFFS, ISNULL((Amount / PtCount) * Pts,0) AS Cap, CONVERT(DECIMAL(6,3),(Pts * 100.0) / (SELECT COUNT(DISTINCT PatID) FROM PatientIns WHERE InscoCode = OD.PaySource)) AS PctPts,CONVERT(DECIMAL(6,3),(Visits * 100.0) / (SELECT COUNT(VoucherNo) FROM Vouchers WHERE Insco = OD.PaySource)) AS PctVisits,ISNULL(((Amount / PtCount) * Pts + ISNULL(FFS,0)) / Visits,0) AS AvgTotVisit FROM/*Problem is with the next two derived tables. Separately, they run OK, when trying to JOIN them, get the error. This error began after putting in the V derived table (see LEFT JOIN (Derived Table) V in the first outer derived table OD*/(SELECT PtI.InscoCode AS Paysource,FromM AS FromMonth,ToM AS ToMonth, #Tbl.Sex, COUNT(DISTINCT PtI.PatID) AS Pts,COUNT(VoucherNo) AS Visits,SUM(TotPd) AS FFS FROM PatientIns PtI LEFT JOIN (SELECT VoucherNo,Patid,Insco,TotPd FROM Vouchers V WHERE (SELECT MAX(ServiceDate) FROM VouchersDetail WHERE Voucher = V.VoucherNo) BETWEEN@Start AND @End) V ON V.PatID = PtI.Patid AND V.Insco = PtI.Inscocode INNER JOIN Patients P ON PtI.Patid = P.ID RIGHT JOIN #Tbl ON DATEDIFF(m,P.DOB,getdate()) BETWEEN #Tbl.FromM AND #Tbl.ToM AND P.Sex = (CASE WHEN #Tbl.Sex <> 'M' AND #Tbl.Sex <> 'F' THEN P.Sex ELSE #Tbl.Sex END)WHERE PtI.Inscocode = @Insco GROUP BY InscoCode,FromM,ToM,#Tbl.SexUNION SELECT @Insco,FromM,ToM,Sex,0,0,NULL FROM #Tbl WHERE NOT EXISTS(SELECT * FROM Patients INNER JOIN PatientIns ON Patid = ID WHERE DATEDIFF(m,DOB,getdate()) BETWEEN #Tbl.FromM AND #Tbl.ToM AND Sex = (CASE WHEN #Tbl.Sex <> 'M' AND #Tbl.Sex <> 'F' THEN Sex ELSE #Tbl.Sex END)AND InscoCode = @Insco))ODLEFT JOIN (SELECT PaySource,SUM(Amount) AS Amount,AVG(PtCount) AS PtCount,(SELECT COUNT(*) FROM Vouchers V INNER JOIN PatientIns PON P.Patid = V.Patid AND V.Insco = P.InscoCode WHERE Insco = OtherPmts.PaySource) AS TotVisits FROM OtherPmts WHERE PaySource = @Insco AND DtFrom BETWEEN @Start AND @EndGROUP BY PaySource)O ON O.PaySource = OD.Paysource Now what?Sarah Berger MCSD |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2003-06-19 : 12:20:39
|
| you're getting the dreaded msg 8624?Jonathan{0} |
 |
|
|
simondeutsch
Aged Yak Warrior
547 Posts |
Posted - 2003-06-19 : 12:31:54
|
| Ayup. Looks like it.Sarah Berger MCSD |
 |
|
|
simondeutsch
Aged Yak Warrior
547 Posts |
Posted - 2003-06-19 : 12:56:52
|
Aaargh...I got it fixed. Changed this:(SELECT PtI.InscoCode AS Paysource,FromM AS FromMonth,ToM AS ToMonth, #Tbl.Sex, COUNT(DISTINCT PtI.PatID) AS Pts,COUNT(VoucherNo) AS Visits,SUM(TotPd) AS FFS FROM PatientIns PtI LEFT JOIN (SELECT VoucherNo,Patid,Insco,TotPd FROM Vouchers V WHERE (SELECT MAX(ServiceDate) FROM VouchersDetail WHERE Voucher = V.VoucherNo) BETWEEN@Start AND @End) V ON V.PatID = PtI.Patid AND V.Insco = PtI.Inscocode INNER JOIN Patients P ON PtI.Patid = P.ID RIGHT JOIN #Tbl ON DATEDIFF(m,P.DOB,getdate()) BETWEEN #Tbl.FromM AND #Tbl.ToM AND P.Sex = (CASE WHEN #Tbl.Sex <> 'M' AND #Tbl.Sex <> 'F' THEN P.Sex ELSE #Tbl.Sex END)WHERE PtI.Inscocode = @Insco GROUP BY InscoCode,FromM,ToM,#Tbl.SexUNION SELECT @Insco,FromM,ToM,Sex,0,0,NULL FROM #Tbl WHERE NOT EXISTS(SELECT * FROM Patients INNER JOIN PatientIns ON Patid = ID WHERE DATEDIFF(m,DOB,getdate()) BETWEEN #Tbl.FromM AND #Tbl.ToM AND Sex = (CASE WHEN #Tbl.Sex <> 'M' AND #Tbl.Sex <> 'F' THEN Sex ELSE #Tbl.Sex END)AND InscoCode = @Insco))OD To this:(SELECT PtI.InscoCode AS Paysource,FromM AS FromMonth,ToM AS ToMonth, #Tbl.Sex, COUNT(DISTINCT PtI.PatID) AS Pts,COUNT(DISTINCT VoucherNo) AS Visits,SUM(AmountPaid) AS FFS FROM PatientIns PtI LEFT JOIN Vouchers V ON V.PatID = PtI.Patid AND V.Insco = PtI.Inscocode LEFT JOIN Vouchersdetail VDON V.VoucherNo = VD.Voucher AND VD.ServiceDate BETWEEN @Start AND @End INNER JOIN Patients P ON PtI.Patid = P.ID RIGHT JOIN #Tbl ON DATEDIFF(m,P.DOB,getdate()) BETWEEN #Tbl.FromM AND #Tbl.ToM AND P.Sex = (CASE WHEN #Tbl.Sex <> 'M' AND #Tbl.Sex <> 'F' THEN P.Sex ELSE #Tbl.Sex END)WHERE PtI.Inscocode = @Insco GROUP BY InscoCode,FromM,ToM,#Tbl.SexUNION SELECT @Insco,FromM,ToM,Sex,0,0,NULL FROM #Tbl WHERE NOT EXISTS(SELECT * FROM Patients INNER JOIN PatientIns ON Patid = ID WHERE DATEDIFF(m,DOB,getdate()) BETWEEN #Tbl.FromM AND #Tbl.ToM AND Sex = (CASE WHEN #Tbl.Sex <> 'M' AND #Tbl.Sex <> 'F' THEN Sex ELSE #Tbl.Sex END)AND InscoCode = @Insco))OD The important thing to note is the removal of the V derived table that caused the brouhaha in the first place.Sarah Berger MCSD |
 |
|
|
|
|
|
|
|