[Solved] SQL Server Case Statement Error: Incorrect syntax near ,

makumbi
 
on Jul 25, 2022 11:07 PM
407 Views

Please help iam trying to use case but iam getting the error below

 iam getting error below

Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ','.

 

SELECT        TOP (100) PERCENT dbo.P3P7.Photo, dbo.P3p3onlypos2.Name, dbo.P3p3onlypos2.Stream, dbo.P3p3onlypos2.admno, dbo.P3p3onlypos2.scen2, dbo.P3p3onlypos2.sst2, dbo.P3p3onlypos2.maths2, dbo.P3p3onlypos2.eng2,
	dbo.P3p3onlypos2.scen3, dbo.P3p3onlypos2.sst3, dbo.P3p3onlypos2.maths3, dbo.P3p3onlypos2.eng3, dbo.P3p3onlypos2.scini, dbo.P3p3onlypos2.sstini, dbo.P3p3onlypos2.Engini, dbo.P3p3onlypos2.mtcini,
	dbo.P3p3onlypos2.Head, dbo.P3p3onlypos2.engremark, dbo.P3p3onlypos2.scienceremark, dbo.P3p3onlypos2.mathsremark, dbo.P3p3onlypos2.sstremark, dbo.P3p3onlypos2.classteachername, dbo.P3p3onlypos2.Total3,
	dbo.P3p3onlypos2.Total2, dbo.P3p3onlypos2.engrade, dbo.P3p3onlypos2.sciencegrade, dbo.P3p3onlypos2.sstgrade, dbo.P3p3onlypos2.mathsgrade, dbo.P3p3onlypos2.Totalagg, dbo.P3p3onlypos2.Class,
	dbo.P3p3onlypos2.Div2, dbo.P3p3onlypos2.Div3, dbo.P3p3onlypos2.outof, dbo.P3p3onlypos2.Pos, dbo.P3p3onlypos2.Headcom, dbo.P3p3onlypos2.classteachercomment, dbo.P3p3onlypos2.CS, dbo.P3p3onlypos2.scen1,
	dbo.P3p3onlypos2.sst1, dbo.P3p3onlypos2.maths1, dbo.P3p3onlypos2.eng1, dbo.P3p3onlypos2.Total1, dbo.P3p3onlypos2.Div1, dbo.P3p3onlypos2.gstotal, dbo.P3p3onlypos2.swa3, dbo.P3p3onlypos2.swgrade,
	dbo.P3p3onlypos2.swremark, dbo.P3p3onlypos2.Swini, dbo.P3p3onlypos2.ire1, dbo.P3p3onlypos2.iregrade1, dbo.P3p3onlypos2.ire2, dbo.P3p3onlypos2.iregrade2, dbo.P3p3onlypos2.ire3, dbo.P3p3onlypos2.iregrade3,
	dbo.P3p3onlypos2.ireremark, dbo.P3p3onlypos2.ireini, dbo.P3p3onlypos2.Status, dbo.P3p3onlypos2.swa2, dbo.P3p3onlypos2.swa1, dbo.P3p3onlypos2.postream, dbo.P3p3onlypos2.Outsream, dbo.P3p3onlypos2.Postatus,
	dbo.P3p3onlypos2.Cscore2, dbo.P3p3onlypos2.Cagg2, dbo.P3p3onlypos2.Cgrade2, dbo.P3p3onlypos2.Rscore2, dbo.P3p3onlypos2.Ragg2, dbo.P3p3onlypos2.Rgrade2, dbo.P3p3onlypos2.Cscore1, dbo.P3p3onlypos2.Cagg1,
	dbo.P3p3onlypos2.Cgrade1, dbo.P3p3onlypos2.Rscore1, dbo.P3p3onlypos2.Ragg1, dbo.P3p3onlypos2.Rgrade1, dbo.P3p3onlypos2.Cscore3, dbo.P3p3onlypos2.Cgrade3, dbo.P3p3onlypos2.Cagg3,
	dbo.P3p3onlypos2.Cremark3, dbo.P3p3onlypos2.Rscore3, dbo.P3p3onlypos2.Rgrade3, dbo.P3p3onlypos2.Ragg3, dbo.P3p3onlypos2.Rremark3, dbo.P3p3onlypos2.Cini, dbo.P3p3onlypos2.Rini, dbo.P3p3onlypos2.promoted,
	dbo.P3p3onlypos2.lug3, dbo.P3p3onlypos2.lugagg, dbo.P3p3onlypos2.Lugremark, dbo.P3p3onlypos2.lug1, dbo.P3p3onlypos2.lugagg1, dbo.P3p3onlypos2.lug2, dbo.P3p3onlypos2.lugagg2, dbo.P3P7.Lini, dbo.P3P7.Nin,
	dbo.Swimming.swim, dbo.Swimming.swimgrade, dbo.Swimming.swimsgrade, dbo.Swimming.swimremark, dbo.Music.music, dbo.Music.msgrade, dbo.Music.mscgrade, dbo.Music.msremark,
                           
	( CASE  WHEN dbo.P3P7.Class IN ('P7') THEN 
                 dbo.gradesSci(dbo.P3p3onlypos2.eng2) ,  dbo.gradesSci(dbo.P3p3onlypos2.sst2) , dbo.gradesSci(dbo.P3p3onlypos2.scen2) , dbo.gradesSci(dbo.P3p3onlypos2.maths2) ,
                 dbo.gradesSci(dbo.P3p3onlypos2.ire2) , dbo.gradesSci(dbo.P3p3onlypos2.Cscore2) , dbo.gradesSci(dbo.P3p3onlypos2.eng1) , dbo.gradesSci(dbo.P3p3onlypos2.sst1) ,
                 dbo.gradesSci(dbo.P3p3onlypos2.scen1) , dbo.gradesSci(dbo.P3p3onlypos2.maths1) , dbo.gradesSci(dbo.P3p3onlypos2.ire1) , dbo.gradesSci(dbo.P3p3onlypos2.Cscore1)
 
        ELSE
        dbo.gradesSci(dbo.P3p3onlypos2.eng2) AS Eg2, dbo.gradesSci(dbo.P3p3onlypos2.sst2) AS sstg2, dbo.gradesSci(dbo.P3p3onlypos2.scen2) AS sceng2, dbo.gradesSci(dbo.P3p3onlypos2.maths2) AS mathg2,
                 dbo.gradesSci(dbo.P3p3onlypos2.ire2) AS ireg2, dbo.gradesSci(dbo.P3p3onlypos2.Cscore2) AS compg2, dbo.gradesSci(dbo.P3p3onlypos2.eng1) AS Eg1, dbo.gradesSci(dbo.P3p3onlypos2.sst1) AS sstg1,
                 dbo.gradesSci(dbo.P3p3onlypos2.scen1) AS sceng1, dbo.gradesSci(dbo.P3p3onlypos2.maths1) AS mathg1, dbo.gradesSci(dbo.P3p3onlypos2.ire1) AS ireg1, dbo.gradesSci(dbo.P3p3onlypos2.Cscore1) AS compg1
 
 
		END
	) 
FROM            dbo.P3P7 INNER JOIN
	dbo.P3p3onlypos2 ON dbo.P3P7.admno = dbo.P3p3onlypos2.admno INNER JOIN
	dbo.Swimming ON dbo.P3p3onlypos2.admno = dbo.Swimming.admno INNER JOIN
	dbo.Music ON dbo.Swimming.admno = dbo.Music.admno
ORDER BY dbo.P3p3onlypos2.Stream, dbo.P3p3onlypos2.Pos

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 26, 2022 12:16 AM
on Jul 26, 2022 12:40 AM

In the case statement you can't select multiple columns.

You have to select only one column.

SELECT TOP (100) PERCENT dbo.P3P7.Photo, dbo.P3p3onlypos2.Name, dbo.P3p3onlypos2.Stream, dbo.P3p3onlypos2.admno, dbo.P3p3onlypos2.scen2, dbo.P3p3onlypos2.sst2, dbo.P3p3onlypos2.maths2, dbo.P3p3onlypos2.eng2,
    dbo.P3p3onlypos2.scen3, dbo.P3p3onlypos2.sst3, dbo.P3p3onlypos2.maths3, dbo.P3p3onlypos2.eng3, dbo.P3p3onlypos2.scini, dbo.P3p3onlypos2.sstini, dbo.P3p3onlypos2.Engini, dbo.P3p3onlypos2.mtcini,
    dbo.P3p3onlypos2.Head, dbo.P3p3onlypos2.engremark, dbo.P3p3onlypos2.scienceremark, dbo.P3p3onlypos2.mathsremark, dbo.P3p3onlypos2.sstremark, dbo.P3p3onlypos2.classteachername, dbo.P3p3onlypos2.Total3,
    dbo.P3p3onlypos2.Total2, dbo.P3p3onlypos2.engrade, dbo.P3p3onlypos2.sciencegrade, dbo.P3p3onlypos2.sstgrade, dbo.P3p3onlypos2.mathsgrade, dbo.P3p3onlypos2.Totalagg, dbo.P3p3onlypos2.Class,
    dbo.P3p3onlypos2.Div2, dbo.P3p3onlypos2.Div3, dbo.P3p3onlypos2.outof, dbo.P3p3onlypos2.Pos, dbo.P3p3onlypos2.Headcom, dbo.P3p3onlypos2.classteachercomment, dbo.P3p3onlypos2.CS, dbo.P3p3onlypos2.scen1,
    dbo.P3p3onlypos2.sst1, dbo.P3p3onlypos2.maths1, dbo.P3p3onlypos2.eng1, dbo.P3p3onlypos2.Total1, dbo.P3p3onlypos2.Div1, dbo.P3p3onlypos2.gstotal, dbo.P3p3onlypos2.swa3, dbo.P3p3onlypos2.swgrade,
    dbo.P3p3onlypos2.swremark, dbo.P3p3onlypos2.Swini, dbo.P3p3onlypos2.ire1, dbo.P3p3onlypos2.iregrade1, dbo.P3p3onlypos2.ire2, dbo.P3p3onlypos2.iregrade2, dbo.P3p3onlypos2.ire3, dbo.P3p3onlypos2.iregrade3,
    dbo.P3p3onlypos2.ireremark, dbo.P3p3onlypos2.ireini, dbo.P3p3onlypos2.Status, dbo.P3p3onlypos2.swa2, dbo.P3p3onlypos2.swa1, dbo.P3p3onlypos2.postream, dbo.P3p3onlypos2.Outsream, dbo.P3p3onlypos2.Postatus,
    dbo.P3p3onlypos2.Cscore2, dbo.P3p3onlypos2.Cagg2, dbo.P3p3onlypos2.Cgrade2, dbo.P3p3onlypos2.Rscore2, dbo.P3p3onlypos2.Ragg2, dbo.P3p3onlypos2.Rgrade2, dbo.P3p3onlypos2.Cscore1, dbo.P3p3onlypos2.Cagg1,
    dbo.P3p3onlypos2.Cgrade1, dbo.P3p3onlypos2.Rscore1, dbo.P3p3onlypos2.Ragg1, dbo.P3p3onlypos2.Rgrade1, dbo.P3p3onlypos2.Cscore3, dbo.P3p3onlypos2.Cgrade3, dbo.P3p3onlypos2.Cagg3,
    dbo.P3p3onlypos2.Cremark3, dbo.P3p3onlypos2.Rscore3, dbo.P3p3onlypos2.Rgrade3, dbo.P3p3onlypos2.Ragg3, dbo.P3p3onlypos2.Rremark3, dbo.P3p3onlypos2.Cini, dbo.P3p3onlypos2.Rini, dbo.P3p3onlypos2.promoted,
    dbo.P3p3onlypos2.lug3, dbo.P3p3onlypos2.lugagg, dbo.P3p3onlypos2.Lugremark, dbo.P3p3onlypos2.lug1, dbo.P3p3onlypos2.lugagg1, dbo.P3p3onlypos2.lug2, dbo.P3p3onlypos2.lugagg2, dbo.P3P7.Lini, dbo.P3P7.Nin,
    dbo.Swimming.swim, dbo.Swimming.swimgrade, dbo.Swimming.swimsgrade, dbo.Swimming.swimremark, dbo.Music.music, dbo.Music.msgrade, dbo.Music.mscgrade, dbo.Music.msremark,                       
    CASE 
	WHEN dbo.P3P7.Class IN ('P7') THEN 
		dbo.gradesSci(dbo.P3p3onlypos2.eng2)
    ELSE
		dbo.gradesSci(dbo.P3p3onlypos2.sst2)
    END
FROM dbo.P3P7 
INNER JOIN dbo.P3p3onlypos2 ON dbo.P3P7.admno = dbo.P3p3onlypos2.admno 
INNER JOIN dbo.Swimming ON dbo.P3p3onlypos2.admno = dbo.Swimming.admno 
INNER JOIN dbo.Music ON dbo.Swimming.admno = dbo.Music.admno
ORDER BY dbo.P3p3onlypos2.Stream, dbo.P3p3onlypos2.Pos