I have received a requirement to generate a fixed length feed file containing user information from the application database and share the file with another system.
In fixed length, it has been told that each column will have x character length followed by the next column of say y character length and so on until the last column to be fetched. The total row length has to be 2900.
I have managed to draft a SQL file which looks something like the below code snippet:
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET LINESIZE 2900
SET PAGESIZE 0
SPOOL user_feed.txt
SELECT extract_date
|| user_name
|| user_email
|| user_sys
|| user_grpname "USER_ROW"
FROM
(SELECT RPAD(DECODE(extract_date,'',' ',trim(extract_date)),26) user_name,
RPAD(DECODE(name,'',' ',trim(name)),50) user_name,
RPAD(DECODE(email,'',' ',trim(email)),75) user_email,
RPAD(DECODE(sys_name,'',' ',trim(sys_name)),10) user_sys,
RPAD(DECODE(group_name,'',' ',trim(group_name)),26) user_grpname
--....so on (picking all columns from below query)
FROM
(SELECT CAST(sysdate AS TIMESTAMP) extract_date,
u.name,
u.email,
g.sys_name,
g.group_name --(....so on)
FROM user_tab u,
user_grp g
WHERE u.userid = g.userid
)
);
SPOOL OFF;
When I spool small amount of data the fixed length file is produced, but when the source tables contain large number of rows, the spool file does not preserve the formatting, the data gets mixed.
- Can anyone please suggest how I can overcome this issue?
- Is there any other method apart from spool that can be used to generate files in Oracle?
Thanks.
