Jeg tror, det er muligt, selvom det er ret kompliceret, at skrive en pipelinet tabelfunktion, der returnerer en variabel struktur . Din pipeline-tabelfunktion vil bruge Oracle Data Cartridge-grænsefladen og magien i AnyDataSet-typen til at returnere en dynamisk struktur under kørsel. Du kan så bruge det i efterfølgende SQL-sætninger, som om det var en tabel, dvs.
SELECT *
FROM TABLE( your_pipelined_function( p_1, p_2 ));
Et par flere referencer, der diskuterer den samme eksempelimplementering
- Dynamisk SQL-pivotering
- Implementering af grænsefladetilgangen afsnittet i Oracle Data Cartridge Developer's Guide
-
Metode4. Efter at have downloadet og installeret open source PL/SQL-koden, er her en komplet implementering:
--Create sample table. create table branch_data as select '100' BranchName,'1001010' CustomerNo from dual UNION ALL select '100' BranchName,'1001011' CustomerNo from dual UNION ALL select '103' BranchName,'1001012' CustomerNo from dual UNION ALL select '104' BranchName,'1001013' CustomerNo from dual UNION ALL select '104' BranchName,'1001014' CustomerNo from dual UNION ALL select '104' BranchName,'1001015' CustomerNo from dual UNION ALL select '105' BranchName,'1001016' CustomerNo from dual UNION ALL select '105' BranchName,'1001017' CustomerNo from dual UNION ALL select '106' BranchName,'1001018' CustomerNo from dual; --Create a dynamic pivot in SQL. select * from table(method4.dynamic_query( q'[ --Create a select statement select --The SELECT: 'select'||chr(10)|| --The column list: listagg( replace(q'!sum(case when BranchName = '#BRANCH_NAME#' then 1 else 0 end) "#BRANCH_NAME#"!', '#BRANCH_NAME#', BranchName) , ','||chr(10)) within group (order by BranchName)||chr(10)|| --The FROM: 'from branch_data' v_sql from ( --Distinct BranchNames. select distinct BranchName from branch_data ) ]' ));