What is wrong with this syntax?
Declare @comparisongroup int;
...
Insert Into @universitytemp Case @comparisongroup
When -1 Then Select * From dbo.University;
When -2 Then Select * From dbo.University Where SubDivisionName = @group;
When -3 Then Select * From dbo.University Where ConferenceName = @group;
Else Select * From dbo.GroupUniversity Where GroupID = CONVERT(int, @group);
End;
This doesn’t work either
Declare @comparisongroup int;
...
Case @comparisongroup
When -1 Then Insert Into @universitytemp Select * From dbo.University
When -2 Then Insert Into @universitytemp Select * From dbo.University Where SubDivisionName = @group
When -3 Then Insert Into @universitytemp Select * From dbo.University Where ConferenceName = @group
Else Insert Into @universitytemp Select * From dbo.GroupUniversity Where GroupID = CONVERT(int, @group)
End;
Neither does this:
Declare @comparisongroup int;
...
Case
When @comparisongroup = -1 Then Insert Into @universitytemp Select * From dbo.University;
When @comparisongroup = -2 Then Insert Into @universitytemp Select * From dbo.University Where SubDivisionName = @group
When @comparisongroup = -3 Then Insert Into @universitytemp Select * From dbo.University Where ConferenceName = @group
Else Insert Into @universitytemp Select * From dbo.GroupUniversity Where GroupID = CONVERT(int, @group)
End;
I get errors saying Incorrect syntax near the keyword 'When', 'Else', and 'End'
That’s because
CASEis an expression that returns a value. A single value. Not a result set.You might try:
I.e. not use a
CASEexpression at all. As a stylistic point, I’d also probably separate@groupinto two parameters/variables – one which has “name” in its name, and continues to be (varchar?), the other of which is explicitly anintand used solely for theGroupIDcomparison.