SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ORACLE: DUMP RESULT IN A TABLE


ORACLE: DUMP RESULT IN A TABLE

Author
Message
nischalinn
nischalinn
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 29
CREATE TABLE TEST_SUM
(
USERID VARCHAR2(10),
DEBIT_AMT NUMBER(19,4),
CREDIT_AMT NUMBER(19,4),
BALANCE_AMT NUMBER(19,4)
)


note: the amount fields in the table is dynamic i.e. here there are only 3 amunt fields but there can be any number of "NUMBER" fields.

INSERT ALL
INTO TEST_SUM VALUES ('001',100,200,300)
INTO TEST_SUM VALUES ('005',500,200,700)
INTO TEST_SUM VALUES ('006',300,200,500)
INTO TEST_SUM VALUES ('003',400,300,700)
SELECT * FROM DUAL;


The below script gives the sum of the amount fields via cursor.

CREATE OR REPLACE PROCEDURE GET_SUM_AMT(VTBL VARCHAR2) AS
TBLCURSOR SYS_REFCURSOR;
TBLSQLSTR VARCHAR2(1000);
IMPORTEDROWS VARCHAR2(1000);
TOTAL_AMT VARCHAR2(1000);
BEGIN
TBLSQLSTR := 'SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME= :VAL_BND
AND
(COLUMN_NAME LIKE UPPER(''%AMT%'') OR COLUMN_NAME LIKE UPPER(''%AMOUNT%''))
AND
DATA_TYPE = ''NUMBER''';
OPEN TBLCURSOR FOR TBLSQLSTR USING VTBL;
LOOP
FETCH TBLCURSOR INTO IMPORTEDROWS;
EXIT WHEN TBLCURSOR%NOTFOUND;
EXECUTE IMMEDIATE 'SELECT SUM( '||IMPORTEDROWS||') FROM '||VTBL INTO TOTAL_AMT;
DBMS_OUTPUT.PUT_LINE(''||IMPORTEDROWS||' = '||TOTAL_AMT);
END LOOP;
CLOSE TBLCURSOR;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001,'AN ERROR WAS ENCOUNTERED - '||SQLCODE||' -ERROR- '||SQLERRM);
END;
/

------------------------------------------------------------------------------------------
-- EXEC GET_SUM_AMT('TEST_SUM');
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------


The result is:

PL/SQL block, executed in 0.266 sec.
DEBIT_AMT = 1300
CREDIT_AMT = 900
BALANCE_AMT = 2200
Total execution time 1.066 sec.
-------****************---------------


But I want the result in tabular form same as we get the result from this query:
SELECT Sum(DEBIT_AMT),Sum(CREDIT_AMT),Sum(BALANCE_AMT) FROM TEST_SUM;
. How can I do it?

LutzM
LutzM
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10701 Visits: 13559
This is usually a SQL Server site.
If you get lucky, there'll be a person lurking around having the ORACLE knowledge required. But that would be random.
For Oracle, asktom.oracle.com is a good resource, if I remember correctly....



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search