Home » RDBMS Server » Server Administration » Does Oracle SQL has crosstab query using transform statement like in Access
Does Oracle SQL has crosstab query using transform statement like in Access [message #373025] Fri, 23 March 2001 10:11 Go to next message
Lisa Cheng
Messages: 2
Registered: March 2001
Junior Member
Hi, Does any body know that Oracle SQL can write a crosstab query using transform... pivot... statement like in Access. Because I need to convert an application using Access database to Oracle. Thanks.
Re: Does Oracle SQL has crosstab query using transform statement like in Access [message #373042 is a reply to message #373025] Fri, 23 March 2001 12:37 Go to previous messageGo to next message
Andrew again...
Messages: 270
Registered: July 2000
Senior Member
from www.revealnet.com/plsql-pipeline

CREATE OR REPLACE FUNCTION tabcount (
sch IN VARCHAR2,
tab IN VARCHAR2)
RETURN INTEGER
IS
/*
|| Generic function utilizing dynamic SQL to return the
|| number of rows in the specified table.
||
|| Author: Steven Feuerstein
|| Source: PL/SQL Pipeline (www.revealnet.com/plsql-pipeline)
||
|| Dependencies:
|| DBMS_SQL -- Oracle dynamic SQL package
*/
cur INTEGER := DBMS_SQL.OPEN_CURSOR;
ignore INTEGER;
retval INTEGER;
BEGIN
DBMS_SQL.PARSE (cur, 'SELECT COUNT(*) FROM ' || sch || '.' || tab, DBMS_SQL.NATIVE);
DBMS_SQL.DEFINE_COLUMN (cur, 1, retval);
ignore := DBMS_SQL.EXECUTE_AND_FETCH (cur);
DBMS_SQL.COLUMN_VALUE (cur, 1, retval);
DBMS_SQL.CLOSE_CURSOR (cur);
RETURN retval;
END;
/

CREATE OR REPLACE PROCEDURE transpose (
source_owner IN VARCHAR2,
source_table IN VARCHAR2,
target_owner IN VARCHAR2,
target_table IN VARCHAR2,
showaction IN BOOLEAN := FALSE
)
IS
/*
|| Procedure to transpose a table (columns to rows,
|| rows to columns). In Oracle7, this means that the
|| table may not have more than 254 rows. In Oracle8,
|| the limit is raised to 1000 rows.
||
|| You must create the transpose table (target_table)
|| before you run this procedure. That way, you get to
|| determine the transposed column names.
||
|| Author: Solomon Yakobson
|| Source: PL/SQL Pipeline (www.revealnet.com/plsql-pipeline)
||
|| Dependencies:
|| tabcount -- see above
|| DBMS_SQL -- Oracle dynamic SQL package
*/
rcount NUMBER := tabcount (source_owner, source_table);
cur INTEGER;
ignore INTEGER;
stmt VARCHAR2(32767);
prefix CHAR(1);

CURSOR colcur (
s_owner ALL_TAB_COLUMNS.owner%TYPE,
s_table ALL_TAB_COLUMNS.table_name%TYPE
)
IS
SELECT column_name
FROM ALL_TAB_COLUMNS
WHERE owner = UPPER (s_owner)
AND table_name = UPPER (s_table)
ORDER BY column_id;
BEGIN
IF rcount > 0
THEN
cur := DBMS_SQL.OPEN_CURSOR;
FOR colrec IN colcur (source_owner, source_table)
LOOP
prefix := NULL;
stmt :=
'INSERT INTO ' || target_owner || '.' || target_table || ' SELECT ';

FOR rnum IN 1 .. rcount
LOOP
stmt :=
stmt ||
prefix ||
'MAX(DECODE(ROWNUM,' ||
rnum ||
',' ||
colrec.column_name ||
',NULL))';

IF rnum = 1
THEN
prefix := ',';
END IF;
END LOOP;

stmt := stmt || ' FROM ' || source_owner || '.' || source_table;

IF showaction
THEN
p.l (stmt);
END IF;

DBMS_SQL.PARSE (cur, stmt, DBMS_SQL.native);

ignore := DBMS_SQL.EXECUTE (cur);

IF showaction
THEN
p.l ('INSERT result', ignore);
END IF;
END LOOP;

DBMS_SQL.CLOSE_CURSOR (cur);
END IF;
END;
/

CREATE TABLE orig_tab (
name VARCHAR2(30),
hobby VARCHAR2(100),
age NUMBER,
birthdate DATE
);

CREATE TABLE trans_tab (
steven VARCHAR2(500),
larry VARCHAR2(500),
solomon VARCHAR2(500)
);

INSERT INTO orig_tab VALUES ('Steven', 'Soccer Referee', '40', '23-SEP-58');
INSERT INTO orig_tab VALUES ('Larry', 'Domination', '54', '17-AUG-44');
INSERT INTO orig_tab VALUES ('Solomon', 'Pipeline', '20', '15-AUG-78');

BEGIN
transpose (USER, 'orig_tab', USER, 'trans_tab');
END;
/

SELECT * FROM trans_tab;
Re: Does Oracle SQL has crosstab query using transform statement like in Access [message #373147 is a reply to message #373025] Thu, 29 March 2001 09:34 Go to previous message
Hari
Messages: 59
Registered: August 2000
Member
Check with new oracle sql functions..
Do you want to select data in matrix form?
or something like that..then
use Rollup and Cube..of oracle 8/8i SQl functions
Previous Topic: Finding Orphan Records
Next Topic: web site
Goto Forum:
  


Current Time: Sat Jun 29 00:34:14 CDT 2024