–1、游標操作
–在選修資料庫中,以Student(sno,sname,ssex,sage,sdept),Course(cno,cname,cpno,credit),SC(sno,cno,grade)表為基礎完成下列游標操作,
–(1)在Student表中定義一個包含sno,sname,ssex,sage,sdept的滾動游標,游標的名稱為“cs-cursor”,并將游標中的資料逐條顯示出來,并讀取第一行資料、最后一行資料,當前行前面的一行資料,當前游標開始的第二行資料,關閉游標,釋放游標,
declare cs_cursor cursor
scroll for select * from student open cs_cursor
fetch next from cs_cursor
fetch prior from spj_cursor --向前一行
fetch first from spj_cursor --游標移到第一行
fetch last from spj_cursor --游標移到最后一行
–(2)在Student表中定義一個所在系部為CS,包含sno,sname,ssex的游標,游標的名稱為“cs-cursor”,并將游標中的絕對位置為2的學生的姓名改為“王楠”,性別改為“女”,
declare cs_cursor2 cursor
scroll for select sno,sname,ssex from student where sdept='CS';
open cs_cursor2;
fetch ABSOLUTE 2 from cs_cursor2 ;
update student set sname='王楠', ssex='女' where current of cs_cursor2;
–(3)在Student表中定義一個包含sno,sname,grade的游標,游標的名稱為“cs-cursor”,并將游標遍歷整個資料表,
declare cs_cursor3 cursor
scroll for select s.sno,sname,grade from student s,sc where s.sno=sc.sno ;
open cs_cursor3;
declare @no char
declare @name char
declare @grade int
fetch NEXT from cs_cursor3 INTO @no,@name,@grade while @@FETCH_STATUS=0
BEGIN
PRINT @no+' '+@name+' '+str(@grade)
fetch NEXT from cs_cursor3 INTO @no,@name,@grade
END;
?
?
close cs_cursor3;
DEALLOCATE cs_cursor3;
–2、存盤器操作
–在選修資料庫中,以Student(sno,sname,ssex,sage,sdept),Course(cno,cname,cpno,credit),SC(sno,cno,grade)表為基礎創建下列存盤程序,
–(1)創建一個向Student表插入資料的存盤程序,該程序需要用5個引數,分別傳遞
sno,sname,ssex,sage,sdept,
CREATE PROCEDURE sp_insert(@sno char,@sname char,@ssex char,@sage int,@sdept char)
AS
insert into student values(@sno,@sname,@ssex,@sage,@sdept);
--CLOSE sp_insert
--DEALLOCATE sp_insert
--exec sp_insert '201825139','莉莉','女',20,'CS';
–(2)創建一個向Course表插入資料的存盤程序,該程序需要用4個引數,分別傳遞cno,cname,cpno,ccredit,
CREATE PROCEDURE cp_insert(@cno char,@cname char,@cpno int,@ccredit char)
AS
insert into course values(@cno,@cname,@cpno,@ccredit);
–(3)創建一個向SC表插入資料的存盤程序,該程序需要用3個引數,分別傳遞sno,cno,grade,
CREATE PROCEDURE scp_insert(@sno char,@cno char,@grade int)
AS
insert into course values(@sno ,@cno ,@grade );
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/229933.html
標籤:其他
上一篇:JDBC資料庫連接與增刪改查
