Home » SQL & PL/SQL » SQL & PL/SQL » Problem with passing values to Function (Oracle 19.0.0.0.0 Windows 64bit)
Problem with passing values to Function [message #690143] Wed, 30 October 2024 14:36 Go to next message
Duane
Messages: 581
Registered: December 2002
Senior Member
I hoping someone can explain this to me.

Anytime I pass a value to "Quote" then "Quote" is null.  If I don't include "Quote" in the select parameter list then "Quote" contains '"'.

I'm missing something and I'm not sure what I'm missing.


Table Data:


CREATE TABLE CITIES
(
  ID    NUMBER(5),
  NAME  VARCHAR2(30 CHAR)
)
;


SET DEFINE OFF;
Insert into CITIES
   (ID, NAME)
 Values
   (1, 'AMSTERDAM');
Insert into CITIES
   (ID, NAME)
 Values
   (2, 'UTRECHT');
Insert into CITIES
   (ID, NAME)
 Values
   (3, 'THE HAGUE');
Insert into CITIES
   (ID, NAME)
 Values
   (4, 'ROTTERDAM');
COMMIT;

SQL Macro Function:


CREATE OR REPLACE function CSVDataTest (InputData dbms_tf.table_t,
                                        Delimiter varchar2 default ',',
                                        Quote     varchar2 default '"',
                                        Escape    varchar2 default '\') return clob sql_macro as
     
  DelimiterRecord varchar2(24) default '||Delimiter||';
  ColumnName      varchar2(200);
  ColumnRecord    varchar2(300);
  HeaderRecord    clob;
  DataRecord      clob;
  SQLStatement    clob;
  
  
  begin
    for i in 1..InputData.column.count 
      loop
        ColumnName := InputData.column(i).description.name;
        
        case
          when InputData.column(i).description.type in (dbms_tf.type_varchar2, 
                                                        dbms_tf.type_char, 
                                                        dbms_tf.type_clob)
            then
              if Quote is not null
                then
                  ColumnRecord := 'replace('||ColumnName||','''||Quote||''','''||Escape||Quote||''')';
                  ColumnRecord := ''''||Quote||'''||'||ColumnRecord||'||'''||Quote||'''';
              
                else
                  ColumnRecord := ColumnName;
              end if;
              
          when InputData.column(i).description.type = dbms_tf.type_number
            then
              ColumnRecord := 'to_char('||ColumnName||')';
              
          when InputData.column(i).description.type = dbms_tf.type_date 
            then
              ColumnRecord := 'to_char('||ColumnName||',''YYYY-MM-DD'')';
              
          when InputData.column(i).description.type = dbms_tf.type_timestamp
            then
              ColumnRecord := 'to_char('||ColumnName||',''YYYY-MM-DD"T"HH24:MI:SS.FF6'')';
              
            else
              null;
        end case;
 
        HeaderRecord := HeaderRecord||'||Delimiter||'||''''||ColumnName||'''';
        DataRecord := DataRecord||DelimiterRecord||ColumnRecord;
      end loop;
    
    HeaderRecord := substr(HeaderRecord, length('||Delimiter||') + 1);
    DataRecord := substr(DataRecord, length(DelimiterRecord) + 1);
   
    SQLStatement := q'[
                       select @HEADER_RECORD@ as csv_data from dual
                       union all
                       select @DATA_RECORD@ as csv_data from CSVData.InputData
                      ]';
   
    SQLStatement := replace(replace(SQLStatement, '@DATA_RECORD@', DataRecord), '@HEADER_RECORD@', HeaderRecord);
   
    return SQLStatement;
  end;
/

Query:


with city as (select *
                 from cities
                   order by 1)
                   
  select * from CSVDataTest(InputData => city);

Result:


"ID","NAME"
1,"AMSTERDAM"
2,"UTRECHT"
3,"THE HAGUE"
4,"ROTTERDAM"





Query:


with city as (select *
                 from cities
                   order by 1)
                   
  select * from CSVDataTest(InputData => city, Quote => '@');

Result:


"ID","NAME"
1,AMSTERDAM
2,UTRECHT
3,THE HAGUE
4,ROTTERDAM

[Updated on: Wed, 30 October 2024 14:38]

Report message to a moderator

Re: Problem with passing values to Function [message #690144 is a reply to message #690143] Wed, 30 October 2024 18:56 Go to previous messageGo to next message
Duane
Messages: 581
Registered: December 2002
Senior Member
Ok, I found some posts about this issue.  Parameters with a Macro don't function the same as parameters in a regular function.

Found a post that Solomon created that explains what's going on.  It would appear the solution is to create an Object to hold the parameters and pass that to the function.  I'll be trying that to see if that helps.
Re: Problem with passing values to Function [message #690145 is a reply to message #690144] Thu, 31 October 2024 01:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68706
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Found a post that Solomon created that explains what's going on

In this case, please post a link to this post to help people find and see the solution.

Re: Problem with passing values to Function [message #690146 is a reply to message #690145] Thu, 31 October 2024 07:16 Go to previous message
Duane
Messages: 581
Registered: December 2002
Senior Member
Here's the post.

https://forums.oracle.com/ords/apexds/post/19c-sql-macro-passed-string-parameters-6647
Previous Topic: Oracle Percentage by Rank(Top Percentages)
Next Topic: Date range comparison with hours, minutes and seconds
Goto Forum:
  


Current Time: Wed Nov 06 19:18:53 CST 2024