日期:2014-05-18  浏览次数:20539 次

求写一个存储过程!
有两张表 绩点(GNO,POINT,MIN,MAX) 主键为GNO
  成绩(SNO,CNO,TNO,GRADE,POINT)主键为(SNO.CNO.TNO)

现需要将B表中的每个GRADE分别和A表中的MIN和MAX相比较,若满足GRADE>=MIN && GRADE<=MAX 则更新B.POINT = A.POINT

我对存储过程不是很清楚,哪位高手能写一个完整的学习学习!!!

------解决方案--------------------
SQL code

if object_id('A') is not null drop table A
go
create table A(
GNO nvarchar(20) not null,
POINT float,
[MIN] int,
[MAX] int
)
go
if object_id('B') is not null drop table B
go
create table B(
SNO nvarchar(20) not null,
CNO nvarchar(20) not null,
TNO nvarchar(20) not null,
GRADE int,
POINT float
)
go
alter table B add constraint [PK_B] primary key clustered(SNO,CNO,TNO) on [PRIMARY]
go

insert into A
    select 'g1',20,1,5
union all select 'g2',30,6,10
go
insert into B
    select 's1','c1','t1',2,0
union all select 's2','c2','t2',7,0
go
if objectproperty(object_id('TestProc'),'IsProcedure')=1
drop procedure TestProc
go
create procedure TestProc
as
update B
set B.POINT=A.POINT
from A
where B.GRADE between A.[MIN] and A.[MAX]
go
exec TestProc
select * from A
select * from B

drop proc TestProc
drop table A
drop table B