⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cursors.sql

📁 Java写的ERP系统
💻 SQL
字号:
/**
 *	Cursor Test
 *	$Id: cursors.sql,v 1.2 2002/04/26 03:21:44 jjanke Exp $
 */
DECLARE
	--	Global Variables
	v_Name			AD_Client.Name%TYPE;
	--	Counter
	v_no			NUMBER;
BEGIN

	--	========== Implicit Cursor ==========
	v_no := 0;
	DECLARE
		-- The Cursor - The table has a Name and a Description column
		CURSOR cur_client	IS
			SELECT	* 
			FROM	AD_Client
			ORDER BY Name;
	BEGIN
		FOR ptr_client IN cur_client LOOP
			v_no := v_no + 1;			--	Counter
			v_Name := ptr_client.Name;	--	Reading data into local variable
	   	END LOOP;
		DBMS_OUTPUT.PUT_LINE('Implicit Cursor #=' || v_no);
	END;


	--	========== Explicit Cursor (1) ==========
	v_no := 0;
	DECLARE
		-- The Cursor - The table has a Name and a Description column
		CURSOR cur_client	IS
			SELECT	* 
			FROM	AD_Client
			ORDER BY Name;
	   	-- 
	--	ptr_client	cur_client%ROWTYPE;	--	could be used too
		ptr_client	AD_Client%ROWTYPE;	--	all columns in cursor - so the same
	BEGIN
		OPEN cur_client;
		LOOP
			FETCH cur_client INTO ptr_client;
			EXIT WHEN cur_client%NOTFOUND;	--	leaves loop
			v_no := v_no + 1;			--	Counter
			v_Name := ptr_client.Name;	--	Reading data into local variable
 		END LOOP;
		CLOSE cur_client;
		DBMS_OUTPUT.PUT_LINE('Explicit Cursor (1) #=' || v_no);
	END;


	--	========== Explicit Cursor (2) ==========
	v_no := 0;
	DECLARE
		-- The Cursor - The table has a Name and a Description column
		CURSOR cur_client	IS
			SELECT	Name, Description
			FROM	AD_Client
			ORDER BY Name;
	   	-- 
		v_Description		AD_Client.Description%TYPE;
	BEGIN
		OPEN cur_client;
		LOOP
			FETCH cur_client INTO v_Name, v_Description;
			EXIT WHEN cur_client%NOTFOUND;	--	leaves loop
			v_no := v_no + 1;			--	Counter
			--	v_Name has data already
 		END LOOP;
		CLOSE cur_client;
		DBMS_OUTPUT.PUT_LINE('Explicit Cursor (2) #=' || v_no);
	END;


	--	========== Explicit Cursor (3) ==========
	v_no := 0;
	DECLARE
		-- The Cursor - The table has a Name and a Description column
		CURSOR cur_client	IS
			SELECT	Name, Description
			FROM	AD_Client
			ORDER BY Name;
	   	-- 
		TYPE t_client IS RECORD
		(
			Name		AD_Client.Name%TYPE,
			Description	AD_Client.Description%TYPE
		);
		ptr_client	t_client;
	BEGIN
		OPEN cur_client;
		LOOP
			FETCH cur_client INTO ptr_client;
			EXIT WHEN cur_client%NOTFOUND;	--	leaves loop
			v_no := v_no + 1;			--	Counter
			v_Name := ptr_client.Name;
 		END LOOP;
		CLOSE cur_client;
		DBMS_OUTPUT.PUT_LINE('Explicit Cursor (3) #=' || v_no);
	END;


	--	========== Explicit Cursor (4) ==========
	v_no := 0;
	DECLARE
		TYPE t_client IS REF CURSOR RETURN AD_Client%ROWTYPE;	-- strong
		cur_client t_client;
		ptr_client AD_Client%ROWTYPE;
	BEGIN
		OPEN cur_client FOR	
			SELECT	*
			FROM	AD_Client
			ORDER BY Name;
		LOOP
			FETCH cur_client INTO ptr_client;
			EXIT WHEN cur_client%NOTFOUND;	--	leaves loop
			v_no := v_no + 1;			--	Counter
			v_Name := ptr_client.Name;
 		END LOOP;
		CLOSE cur_client;
		DBMS_OUTPUT.PUT_LINE('Explicit Cursor (4) #=' || v_no);
	END;


	--	========== Explicit Cursor (5) ==========
	v_no := 0;
	DECLARE
		TYPE t_client IS REF CURSOR;		--	weak cursor variable
		cur_client t_client;
		v_Description		AD_Client.Description%TYPE;
	BEGIN
		OPEN cur_client FOR	
			SELECT	Name, Description
			FROM	AD_Client
			ORDER BY Name;
		LOOP
			FETCH cur_client INTO v_Name, v_Description;
			EXIT WHEN cur_client%NOTFOUND;	--	leaves loop
			v_no := v_no + 1;			--	Counter
			--	v_Name has data already
 		END LOOP;
		CLOSE cur_client;
		DBMS_OUTPUT.PUT_LINE('Explicit Cursor (5) #=' || v_no);
	END;


	--	========== Explicit Cursor (6) ==========
	v_no := 0;
	DECLARE
		TYPE t_client IS REF CURSOR;
		cur_client t_client;
		v_Description		AD_Client.Description%TYPE;
		v_cmd				VARCHAR2(2000) := 'SELECT Name, Description '
			|| 'FROM AD_Client '
			|| 'ORDER BY Name';
	BEGIN
		OPEN cur_client FOR	v_cmd;
		LOOP
			FETCH cur_client INTO v_Name, v_Description;
			EXIT WHEN cur_client%NOTFOUND;	--	leaves loop
			v_no := v_no + 1;			--	Counter
			--	v_Name has data already
 		END LOOP;
		CLOSE cur_client;
		DBMS_OUTPUT.PUT_LINE('Explicit Cursor (6) #=' || v_no);
	END;

	--	=======================================================================

	--	========== Implicit Cursor with Parameter ==========
	v_no := 0;
	DECLARE
		-- The Cursor - The table has a Name and a Description column
		CURSOR cur_client (par_length NUMBER)	IS
			SELECT	* 
			FROM	AD_Client
			WHERE	LENGTH(Name) > par_length
			ORDER BY Name;
	BEGIN
		FOR ptr_client IN cur_client(10) LOOP
			v_no := v_no + 1;			--	Counter
			v_Name := ptr_client.Name;	--	Reading data into local variable
	   	END LOOP;
		DBMS_OUTPUT.PUT_LINE('Implicit Cursor #=' || v_no);
	END;


	--	========== Explicit Cursor (1) ==========
	v_no := 0;
	DECLARE
		-- The Cursor - The table has a Name and a Description column
		CURSOR cur_client (par_length NUMBER)	IS
			SELECT	* 
			FROM	AD_Client
			WHERE	LENGTH(Name) > par_length
			ORDER BY Name;
	   	-- 
	--	ptr_client	cur_client%ROWTYPE;	--	could be used too
		ptr_client	AD_Client%ROWTYPE;	--	all columns in cursor - so the same
	BEGIN
		OPEN cur_client(10);
		LOOP
			FETCH cur_client INTO ptr_client;
			EXIT WHEN cur_client%NOTFOUND;	--	leaves loop
			v_no := v_no + 1;			--	Counter
			v_Name := ptr_client.Name;	--	Reading data into local variable
 		END LOOP;
		CLOSE cur_client;
		DBMS_OUTPUT.PUT_LINE('Explicit Cursor (1) #=' || v_no);
	END;


	--	========== Explicit Cursor (6) ==========
	v_no := 0;
	DECLARE
		TYPE t_client IS REF CURSOR;
		cur_client t_client;
		v_Description		AD_Client.Description%TYPE;
		v_cmd				VARCHAR2(2000) := 'SELECT Name, Description '
			|| 'FROM AD_Client '
			|| 'WHERE LENGTH(Name) > :par '
			|| 'ORDER BY Name';
	 	v_parameter			NUMBER := 10;
	BEGIN
		OPEN cur_client FOR	v_cmd USING v_parameter;
		LOOP
			FETCH cur_client INTO v_Name, v_Description;
			EXIT WHEN cur_client%NOTFOUND;	--	leaves loop
			v_no := v_no + 1;			--	Counter
			--	v_Name has data already
 		END LOOP;
		CLOSE cur_client;
		DBMS_OUTPUT.PUT_LINE('Explicit Cursor (6) #=' || v_no);
	END;

END;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -