How to Give Grading based on Total Marks using SqlServer?

Syedshakeer
Posted by Syedshakeer under Sql Server category on | Views : 3108
Below code is the simple example for understanding the purpose of CASE statement and If Exists
Creating a Table:-

create table stud(id int,eng int,comp int,total int,grade char(1))


declare @id_insert as integer
declare @id as integer
declare @eng as integer
declare @comp as integer
declare @total as integer
set @id=34
set @eng=84
set @comp=89
--checking the id is already exists are not in a table
if exists(select id from stud where id=@id)
begin
select 'records exits'
end
else
begin
select 'records not exists'
insert into stud(id,eng,comp) values(@id,@eng,@comp)
set @total=@eng+@comp
update stud set total=@total, grade=CASE
when (@total) > 160 then 'A'
when (@total) > 120 and @total<160 then 'B'
when (@total) > 80 and @total<120 then 'C' end where id=@id
select * from stud
end

Comments or Responses

Login to post response