一个关于cursor的题,求助~~
In the task,you will complete a cursor that inserts data into a table,When complete,the script creats a cursor that read every type from the titles table,determines the average price for titles of titles of that type.and then inserts the information into the types table.
The script is supposed to do the following:
a. Create a read-only cursor called type_insert on titles that
queries distinct data in the type column.
b. Declare two variables:a char(12) value called @type and a
money variable called @avg_price.
c. Open the cursor and fetch the first row into a targer list.
d. While there are rows remaining,calulate the average price for the fetched type,insert the appropriate information into the
types table,and fetch the next row.
e. Display the data in the types table.
题目就是这样的,用到pubs2里的titles表,这个题里需要新建一个types表,从titles里取一条数据,处理之后就放在types里,请会的人帮我写一下d的语句吧,我实在不会了。谢谢
f. Close and deallocata the cursor.
[解决办法]
给你一个相近的例子吧,可以参考一下,流程都是这样的
declare @clbm varchar(20),@clph varchar(20),@cldj decimal(12,6),@clje decimal(15,2)
declare cur_fdj cursor for
select a.clbm,a.clph,b.cldj,b.cldj*a.cksl
from u_material_ckckd a,u_material_dhjyrkjsd b
where (a.clbm=b.clbm) and (a.clph=b.scbh)
open cur_fdj
fetch into cur_fdj
@clbm,@clph,@cldj,@clje
while @@sqlstatus=0
begin
update u_material_ckckd
set
cldj=@cldj,
clje=@clje
where (clbm=@clbm) and (clph=@clph)
end
fetch into cur_fdj
@clbm,@clph,@cldj,@clje
close cur_fdj
;