Home » SQL & PL/SQL » SQL & PL/SQL » SUM & Cursor (Oracle Database 11.2.0.1; Centos)
SUM & Cursor [message #640224] Fri, 24 July 2015 01:48 Go to next message
nqtrung
Messages: 25
Registered: April 2007
Junior Member
Hi all

I have 4 below queries:

Query 1:
DECLARE
	v_sal	NUMBER;
BEGIN
	SELECT SUM(SAL) INTO v_sal FROM EMP E
	INNER JOIN DEPT D ON (E.DEPTNO=D.DEPTNO)
	WHERE D.DNAME IN ('ACCOUNTING','RESEARCH','SALES','OPERATIONS');
	DBMS_OUTPUT.PUT_LINE('Toatl Sal:  ' || v_sal);
END;
/

Result 1: 29025

Query 2:
DECLARE
	CURSOR c1(dname VARCHAR) IS
		SELECT SUM(SAL) FROM EMP E 
		INNER JOIN DEPT D ON (E.DEPTNO=D.DEPTNO) 
		WHERE D.DNAME=dname;
	v_sal	NUMBER;
BEGIN
	OPEN c1('RESEARCH');
	FETCH c1 INTO v_sal;
	CLOSE c1;
	DBMS_OUTPUT.PUT_LINE('Toatl Sal:  ' || v_sal);
END;
/

Result 2: 29025

Query 3:
DECLARE
	CURSOR c1(p_dname VARCHAR) IS
		SELECT SUM(SAL) FROM EMP E 
		INNER JOIN DEPT D ON (E.DEPTNO=D.DEPTNO) 
		WHERE D.DNAME=p_dname;
	v_sal	NUMBER;
BEGIN
	OPEN c1('RESEARCH');
	FETCH c1 INTO v_sal;
	CLOSE c1;
	DBMS_OUTPUT.PUT_LINE('Toatl Sal:  ' || v_sal);
END;
/

Result 3: 10875

Query 4:
DECLARE
	v_sal	NUMBER;
BEGIN
	SELECT SUM(SAL) INTO v_sal FROM EMP E 
	INNER JOIN DEPT D ON (E.DEPTNO=D.DEPTNO) 
	WHERE D.DNAME='RESEARCH';
	DBMS_OUTPUT.PUT_LINE('Toatl Sal:  ' || v_sal);
END;
/

Result 4: 10875

I think that query 2 must return the result as same as result 3, but not it returns the result as result 1. I don't understand the reason why the query 2 didn't apply condition for OPEN c1('RESEARCH'). Pls explain for me, thanks so much

[Updated on: Fri, 24 July 2015 01:53] by Moderator

Report message to a moderator

Re: SUM & Cursor [message #640225 is a reply to message #640224] Fri, 24 July 2015 01:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

Don't you think your post looks better now just adding code tags?
Re: SUM & Cursor [message #640226 is a reply to message #640224] Fri, 24 July 2015 01:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

In query 2 "WHERE D.DNAME=dname" means "WHERE D.DNAME=D.DNAME", the table column has a higher priority than the parameter.

Re: SUM & Cursor [message #640227 is a reply to message #640224] Fri, 24 July 2015 01:57 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
nqtrung wrote on Fri, 24 July 2015 12:18
[code]
WHERE D.DNAME=dname;


dname is the column name used here and not the parameter. It is just like 1 = 1, always true.
Re: SUM & Cursor [message #640228 is a reply to message #640225] Fri, 24 July 2015 01:57 Go to previous message
nqtrung
Messages: 25
Registered: April 2007
Junior Member
Thanks for reminding, Michel Cadot
Previous Topic: how to trapping error in SQL using SYS.DBMS_XMLGEN
Next Topic: Transpose columns
Goto Forum:
  


Current Time: Wed Jul 29 18:19:29 CDT 2026