Вы можете попробовать триггер INSTEAD OF INSERT, например:
create table students(id varchar(10) primary key not null, Department varchar(2), Name varchar(80))
go
create trigger students_insert_PK
on students
INSTEAD OF INSERT
as
declare @id int;
declare @dept varchar(2); select @dept=Department from INSERTED;
select @id=cast(max(right(id,4)) as int) from students where Department=@dept;
set @id=isnull(@id,0)+1;
insert into students
select Department+right('0000'+cast(@id as varchar(4)),4)
, Department
, name
from INSERTED;
go
insert into students (Department,Name) values ('CS','John');
insert into students (Department,Name) values ('CS','Pat');
insert into students (Department,Name) values ('CS','Sheryl');
insert into students (Department,Name) values ('IT','Phil');
insert into students (Department,Name) values ('EE','Frank');
insert into students (Department,Name) values ('EE','Amy');
insert into students (Department,Name) values ('EE','Stu');
go
select * from students;
go
Результаты: