Question : SQL 2005 Query Help - Incorrect syntax near ';'.

First and foremost, the SQL is handled dynamically by the server - therefore, the coding in the WHERE clause will look odd to you. Please ignore this, as its not the issue.

I am trying to get my report to insert a new row in my dataset and when I try to UNION in the next I get this error:

Msg 102, Level 15, State 1, Line 102
Incorrect syntax near ';'.
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:
SET NOCOUNT ON

/********* Determine the beginning of the fiscal year ***************************/

DECLARE 
	@otherlong INT
SELECT
	@otherlong = ISNULL(otherlong , 1)
FROM
	Medlists
WHERE
	tablename = 'fiscalyear'

/***********************************************************************************/

; WITH CTE AS (

--- Bring in the Charges

SELECT
	pv.TicketNumber,
	pv.Visit,
	'01' AS EntityCode,
	RIGHT(YEAR(DATEADD(m , -( @otherlong - 1 ) , Visit)) , 2) AS [Fiscal Year],
	LEFT( CONVERT(VARCHAR, DATEADD(m , -( @otherlong - 1 ) , Visit), 101), 2) AS [Fiscal Period],
	'GL' AS [Subsystem],
	'JRNENT' AS [Source Code],
	LEFT(ISNULL(doc.Ledger , '') + SPACE(10) , 10) + LEFT(LEFT(ISNULL(fac.Ledger , '') , 2) + LEFT(ISNULL(fin.Ledger , '') , 2) + SPACE(10) , 10) AS [DeptSubAccount],
	LEFT(CONVERT(varchar, SUM(pvp.totalfee)) + SPACE(22), 22) AS [Amount],	
	'C' AS [Debit/Credit],
	'Centricity Entry' + SPACE(34) AS [Description],
	SPACE(6) AS [Proj Id],
	SPACE(16) AS [Filler]

FROM
	PatientVisit pv 
	INNER JOIN DoctorFacility comp ON pv.CompanyId = comp.DoctorFacilityId 
	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId 
	INNER JOIN DoctorFacility doc ON pv.DoctorId = doc.DoctorFacilityId 
	LEFT OUTER JOIN Medlists fin ON pv.FinancialClassMId = fin.MedListsId 
	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId 
	INNER JOIN Batch b ON pvp.BatchID = b.BatchID 
	INNER JOIN PatientVisitProcsAgg pvpa ON pvp.PatientVisitProcsID = pvpa.PatientVisitProcsID 
	LEFT OUTER JOIN Procedures p ON pvp.ProceduresID = p.ProceduresID 
	LEFT JOIN MedLists Procs ON p.DepartmentMId = procs.MedListsId

WHERE 
	b.entry >= ISNULL('07/01/2010','1/1/1900') AND b.Status <> 0
	AND b.entry < DATEADD(DAY,1,ISNULL('07/31/2010','1/1/3000')
	)	
	AND  --Filter on Facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Company
	(
	(NULL IS NOT NULL AND pv.CompanyID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Financial Class
	(
	(NULL IS NOT NULL AND pv.FinancialClassMID IN (NULL)) OR
	(NULL IS NULL)
	)

GROUP BY
	pv.TicketNumber,
	pv.Visit,
	doc.Ledger,
	fac.Ledger,
	fin.Ledger

HAVING
	SUM(pvp.TotalFee) <> 0
)	

SELECT * FROM CTE

UNION ALL

SELECT 
	[TicketNumber],
	[Visit],
	'01' AS EntityCode,
	RIGHT(YEAR(DATEADD(m , -( @otherlong - 1 ) , Visit)) , 2) AS [Fiscal Year],
	LEFT( CONVERT(VARCHAR, DATEADD(m , -( @otherlong - 1 ) , Visit), 101), 2) AS [Fiscal Period],
	'GL' AS [Subsystem],
	'JRNENT' AS [Source Code],
	'11504075' + SPACE(12) AS [DeptSubAccount],	
	LEFT(ISNULL([Amount],22)+ SPACE(22), 22) AS [Amount],
	'D' AS [Debit/Credit],
	'Centricity Entry' + SPACE(34) AS [Description],
	SPACE(6) AS [Proj Id],
	SPACE(16) AS [Filler]
FROM
	CTE
	
UNION ALL

; WITH CTF AS (
	
SELECT
	pv.TicketNumber,
	pv.Visit,
	'01' AS EntityCode,
	RIGHT(YEAR(DATEADD(m , -( @otherlong - 1 ) , b.Entry)) , 2) AS [Fiscal Year],
	LEFT(CONVERT(VARCHAR, DATEADD(m , -( @otherlong - 1 ) , b.Entry), 101), 2) AS [Fiscal Period],
	'GL' AS [Subsystem],
	'JRNENT' AS [Source Code],	
	LEFT(ISNULL(pymt.Ledger , '') + SPACE(20) , 20) AS [DeptSubAccount], 
	LEFT(CONVERT(varchar, t.amount) + SPACE(22), 22) AS [Amount],
	'D' AS [Debit/Credit],
	'Centricity Entry' + SPACE(34) AS [Description],
	SPACE(6) AS [Proj Id],
	SPACE(16) AS [Filler]

FROM
	PatientVisit pv 
	INNER JOIN DoctorFacility comp ON pv.CompanyId = comp.DoctorFacilityId 
	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId 
	INNER JOIN DoctorFacility doc ON pv.DoctorId = doc.DoctorFacilityId 
	LEFT OUTER JOIN Medlists fin ON pv.FinancialClassMId = fin.MedListsId 
	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId
	INNER JOIN VisitTransactions vt ON pv.PatientVisitId = vt.PatientVisitid 
	INNER JOIN PaymentMethod pm ON vt.PaymentMethodId = pm.PaymentMethodId 
	INNER JOIN Batch b ON pm.BatchID = b.BatchID 
	INNER JOIN Transactions t ON vt.VisitTransactionsId = t.VisitTransactionsId 
	LEFT OUTER JOIN MedLists at ON t.ActionTypeMId = at.MedListsId 
	LEFT OUTER JOIN (select * from medlists where tablename = 'PaymentTypes') pymt ON t.ActionTypeMId  = pymt.MedlistsId 
WHERE 
	t.Action = 'P' AND b.Status <> 0
	AND NOT EXISTS (SELECT 1 FROM PaymentMethod pm1 WHERE  vt.PaymentMethodId = pm1.PaymentMethodId  AND pm1.InsuranceTransfer = 1)
	AND b.entry >= ISNULL('07/01/2010','1/1/1900') 
	AND b.entry < DATEADD(DAY,1,ISNULL('07/31/2010','1/1/3000')
	)	
	AND  --Filter on Facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Company
	(
	(NULL IS NOT NULL AND pv.CompanyID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Financial Class
	(
	(NULL IS NOT NULL AND pv.FinancialClassMID IN (NULL)) OR
	(NULL IS NULL)
	)

GROUP BY
	pv.TicketNumber,
	b.Entry,
	t.Amount,
	pv.Visit,
	doc.Ledger,
	fac.Ledger,
	fin.Ledger,
	pymt.Ledger
	
HAVING
	t.amount <> 0
)

SELECT * FROM CTF

Answer : SQL 2005 Query Help - Incorrect syntax near ';'.

you have, in abstract:

;WITH alias1 as ( ... )
SELECT ... FROM alias1
UNION ALL
; with alias2 as ( ... )
SELECT ... FROM alias2

you have to do the WITH at once, aka:

;WITH alias1 as ( ... )
, alias2 as ( ... )
SELECT ... FROM alias1
UNION ALL
SELECT ... FROM alias2

Random Solutions  
 
programming4us programming4us