CREATE PROCEDURE Insert_Score 
	@yearno char(4),
	@termno char(2),
	@classno char(10),
	@courseno char(10),
	@achievetype char(10),
	@startNo char(10),
	@Endno char(10)
AS
	
	declare @cnt int,@totalNum int

	if len(rtrim(@startno)) = 0
	     begin
  		select  @cnt = count(*)  from score 
			where ѧ in (select student.ѧ from student where rtrim() = @classno) and ѧ=@termno and ѧ=@yearno and rtrim(ɼ) = rtrim(@achievetype)
		if @cnt <= 0
		  insert score ( ѧ,,ѧ,ѧ,ɼ)
		       select student.ѧ,student.,@yearno,@termno,@achievetype from student where rtrim(student.) = rtrim(@classno)
	
	     end 
	else
	     begin
		--ҳѧŷΧ
		select @totalNum = count(*) from student where ѧ>=@startNo and ѧ<=@EndNo
		select  @cnt = count(*)  from score 
		where  ѧ>=@startNo and ѧ<=@EndNo and ѧ=@termno and ѧ=@yearno and rtrim(ɼ) = rtrim(@achievetype)
		
		
		if @cnt <= 0  --ɼû1ѧĳɼϣȫ
		  insert score ( ѧ,,ѧ,ѧ,ɼ)
		       select student.ѧ,student.,@yearno,@termno,@achievetype from student where  ѧ>=@startNo and ѧ<=@EndNo
	
		if @totalNum - @cnt > 0 --һѧɼû
		  insert score ( ѧ,,ѧ,ѧ,ɼ)
		       select student.ѧ,student.,@yearno,@termno,@achievetype from student 
				where  ѧ>=@startNo and ѧ<=@EndNo and ѧ not in (select ѧ from score where ѧ=@termno and ѧ=@yearno and rtrim(ɼ) = rtrim(@achievetype))
	
	  	
	     end 
