1. ホーム
  2. データベース
  3. 神託

Oracle Dbeaver ストアドプロシージャの構文説明

2022-01-18 15:02:02

可視化ツール Dbeaver

基本的な構文

追加

CREATE OR REPLACE PROCEDURE addStudent
IS 
BEGIN
	INSERT INTO student values(6,2,5,'Xiao Chen',22,0);
END addStudent; 

call addStudent();

DROP procedure getStudent 
SELECT * FROM student


削除

CREATE OR REPLACE PROCEDURE delStudent
IS 
BEGIN
	DELETE FROM student WHERE ID='6';
END delStudent; 

call delStudent();

DROP procedure delStudent 
SELECT * FROM student


変更

CREATE OR REPLACE PROCEDURE updateStudent
IS 
BEGIN
	UPDATE student SET AGE=25 WHERE ID='5';
END updateStudent; 

call updateStudent();

DROP procedure updateStudent 
SELECT * FROM student


単一クエリ

CREATE OR REPLACE PROCEDURE getStudentCount
(studentCount OUT NUMBER)
IS 
BEGIN
	SELECT count(*) INTO studentCount FROM student;
END getStudentCount; 

DECLARE
studentCount NUMBER(38);
BEGIN
	getStudentCount(studentCount);
dbms_output.put_line(studentCount);
END;

DROP procedure getStudentCount 
SELECT * FROM student


複数行のクエリ

-- Define a procedure that returns a cursor
CREATE OR REPLACE PROCEDURE getAllStudent(resule OUT sys_refcursor) IS --returns a cursor
BEGIN 
	OPEN resule FOR SELECT * FROM student;
END;

--query stored procedure
DECLARE
	cur SYS_REFCURSOR; -- Cursor
	result_row student%rowtype;  
BEGIN
	getAllStudent(cur);
	LOOP
		FETCH cur INTO result_row ;
		EXIT WHEN cur%notfound;
		dbms_output.put_line('ID: '||result_row.ID||' TID: '||result_row.TID||' SID: '||result_row.SID||' SNAME: '||result_row.SNAME||' AGE: '|| result_row.AGE||' SEX: '||result_row.SEX);
	END LOOP;
	CLOSE cur;
END;

DROP procedure getAllStudent 
SELECT * FROM student


springbootでの使用



STUDENTテーブル、TEACHERテーブル

先生を削除して、その生徒をすべて削除するビジネスがあります。

CREATE OR REPLACE PROCEDURE delTeacher(myTID IN VARCHAR2)
IS 
BEGIN
	DELETE FROM teacher WHERE TID=myTID;
END delTeacher; 


CREATE OR REPLACE PROCEDURE delStudentOfTeacher(myTID IN VARCHAR2)
IS 
BEGIN
	DELETE FROM student WHERE TID=myTID;
END delStudentOfTeacher; 


<delete id="teacherDelete" parameterType="int">
        {call delTeacher(#{arg0})}
</delete>


<delete id="studentOfTeacherDelete" >
        {call delStudentOfTeacher(#{arg0}) }
</delete>


テスト

@Test
    void contextLoads() {
        teacherService.deleteTeacher(2);
    }



増加

CREATE OR REPLACE PROCEDURE addTeacher(myTID IN varchar2,myTNAME IN varchar2,myAGE IN varchar2)
IS 
BEGIN
	INSERT INTO teacher values(myTID,myTNAME,myAGE);
END addTeacher; 


<insert id="teacherAdd">
        call addTeacher(#{arg0},#{arg1},#{arg2})
</insert>


変更

CREATE OR REPLACE PROCEDURE updateTeacher(myTNAME IN varchar2,myAGE IN varchar2,myTID IN varchar2)
IS 
BEGIN
	UPDATE teacher SET TNAME=myTNAME,AGE=myAGE WHERE TID=myTID;
END updateTeacher; 


<update id="teacherUpdate" >
        call updateTeacher(#{arg0},#{arg1},#{arg2});
</update>


生徒の追加、削除、変更

追加

CREATE OR REPLACE PROCEDURE addStudent(myID IN varchar2,myTID IN varchar2,mySID IN varchar2,mySNAME IN varchar2,myAGE IN number,mySEX IN varchar2)
IS 
BEGIN
	INSERT INTO student values(myID,myTID,mySID,mySNAME,myAGE,mySEX);
END addStudent; 


<insert id="studentAdd" >
        call addStudent(#{arg0},#{arg2},#{arg1},#{arg3},#{arg4},#{arg5})
</insert>


削除

CREATE OR REPLACE PROCEDURE delStudent(mySID IN varchar2)
IS 
BEGIN
	DELETE FROM student WHERE SID=mySID;
END delStudent; 


<delete id="studentDelete" >
        call delStudent(#{arg0})
</delete>


変更

CREATE OR REPLACE PROCEDURE updateStudent(mySID IN varchar2,mySNAME IN varchar2,myAGE IN NUMBER,mySEX IN varchar2)
IS 
BEGIN
	UPDATE student SET SNAME=mySNAME,AGE=myAGE,SEX=mySEX WHERE SID=mySID;
END updateStudent; 


この記事は、Oracle Dbeaverストアドプロシージャについて紹介されています、より関連するOracle Dbeaverストアドプロシージャの内容は、スクリプトハウスの過去の記事を検索してくださいまたは、次の関連記事を閲覧を続けるには、今後よりスクリプトハウスをサポートしています願っています