[Informix][SQL]建立、刪除、執行Stored Procedure、Debug。(此例有foreach,依傳入參數從TableA取值,逐筆抄入TableB)

  • 1148
  • 0

.

--刪除已有的PROCEDURE(要按照傳入參數型別,一個個指定,但不用指定資料長度)
--DROP PROCEDURE GetherBoys(char,char);


----↓中間這段須選取起來,用整塊執行--------------------------------------------------------------------
--想要Debug,最快捷的方式是利用return。記得要改變傳回變數型別及長度。
--建立
create procedure GetherBoys(p_school char(50),p_grade char(1))
               returning integer,varchar(30),varchar(30);	--傳出資料型別及長度。如果用return來debug,記得要改這裡。
	define v_rscount integer;
	define v_StdID,v_StdName varchar(30);					--欲用來return的變數,須注意要符合傳出資料型別及長度
	foreach
		select StdID,StdName 
			into v_StdID,v_StdName	--變數接Table的值
			from classroomA 
			where gender=1
			and School=p_school
			and Grade=p_grade	--注意這裡不能有;結尾!
		
			--====↓在foreach區塊裡的陳述式各要有;結尾=============================================================
			let v_rscount=v_rscount+1;	--直接給變數值
			
			if v_rscount>=0 then
				--====↓在if區塊裡的陳述式各要有;結尾=============================================================
				insert into classroomBoy (StdID,StdName) values(v_StdID,v_StdName);
				--====↑在if區塊裡的陳述式各要有;結尾=============================================================
			end if;
			
			return v_rscount,v_StdID,v_StdName with resume;		--return可以在procedure中間,也可以在最後。只能return一次。return傳回值後會繼續往下執行
																--return結尾處有"with resume"表示傳回結果會一筆一筆堆起來,看起來像Table,但沒有欄位名稱
			--====↑在foreach裡的陳述式各要有;結尾=============================================================
	
	end foreach;	--foreach區塊結尾,要有;
	
	--return v_rscount,v_StdID,v_StdName with resume;		--return可以在procedure中間,也可以在最後。只能return一次。
															--return結尾處無"with resume"表示傳回結果只有一筆。沒有欄位名稱。
end procedure;   
----↑中間這段須用整塊執行----------------------------------------------------------


--執行
--execute procedure GetherBoys('快樂國小','5');