pvsbandi
asked on
Fixed Length File from DB2 SQL
Hi,
I have a DB2 UDB 9.7 SQL script, as follows:
I need to pass the script into Unix and generate a fixed length file from this.
Can someone kindly provide a script to achieve it?
I have a DB2 UDB 9.7 SQL script, as follows:
I need to pass the script into Unix and generate a fixed length file from this.
Can someone kindly provide a script to achieve it?
SELECT
CAST(COALESCE(CL_ID,'000000000') AS CHAR(9)) AS CL_ID
,STATUS
,CAST(COALESCE(PLACEMENT_CATEGORY,'') AS CHAR(3)) AS PLACEMENT_CATEGORY
,CAST(COALESCE(DOB_DT,'') AS CHAR(8)) AS DOB_DT
,CAST(COALESCE(LDSS,'') AS CHAR(3)) AS LDSS
,CAST(COALESCE(FIRST_NM,'') AS CHAR(20)) AS FIRST_NM
,CAST(COALESCE(LAST_NM,'') AS CHAR(20)) AS LAST_NM
,CAST(COALESCE(MIDDLE_INITIAL,'') AS CHAR(1)) AS MIDDLE_INITIAL
,CAST(COALESCE(STREET_NUMBER,'') AS CHAR(5)) AS STREET_NUMBER
,CAST(COALESCE(Street_nm,'') AS CHAR(20)) AS STREET_NM
,CAST(COALESCE(additional_Street_,'') AS CHAR(20)) AS additional_Street_Nm
,CAST(COALESCE(CITY_NM,'') AS CHAR(15)) AS CITY_NM
,CAST(COALESCE(STATE_CD,'') AS CHAR(2)) AS STATE_CD
,CAST(COALESCE(PA_ADR_ZIP5_NO,'') AS CHAR(5)) AS PA_ADR_ZIP5_NO
,CAST(COALESCE(SSN_NO,'000000000') AS CHAR(9)) AS SSN_NO
,CAST(COALESCE(school_name,'') AS CHAR(10)) AS school_name
FROM SL_JUN2014
you don't need a script
all you need is to run the db2 command have it output to a file (i think you can set some flags to strip all the headers)
if that does not work try to use the export command (i think that is the name of the command, haven't used it in a while...)
all you need is to run the db2 command have it output to a file (i think you can set some flags to strip all the headers)
if that does not work try to use the export command (i think that is the name of the command, haven't used it in a while...)
ASKER
Momi, i have been using the export command with pipe delimiter first. Then i'm redirecting the result to an out file. Then i open the file and substitute all the double quotes around each field with blank.
Then i replace all the pipe characters with blank.
I want yo be able to create a script that does all that.
Then i replace all the pipe characters with blank.
I want yo be able to create a script that does all that.
The Q editor can do this
Fetch the tar.gz
tar xf q-19.tar.gz
cd q-19/q
make
cp q /usr/local/bin (or ~/bin if you have one)
21:47:17$ cat t5
"had quotes"
a | pipe
21:47:22$ q -o -q -i "y \" ' '^Jy | ' '^Js^Jq^J" t5
21:48:23$ cat t5
had quotes
a pipe
21:48:57$
You can get the Q editor from https://github.com/duncan-roe/q/releases/tag/v19Fetch the tar.gz
tar xf q-19.tar.gz
cd q-19/q
make
cp q /usr/local/bin (or ~/bin if you have one)
ASKER
I cannot install new software at my client location,
Is it not doable by a script at all?
Is it not doable by a script at all?
Sorry, posted wrong answer.
BTW: Why did your script not work?
BTW: Why did your script not work?
Ok I think I understood now.
If the name of your output file will be "sl_jun2014_out.txt", try this:
If the name of your output file will be "sl_jun2014_out.txt", try this:
$ db2 -rsl_jun2014_out.txt <<EOF
CONNECT ...
SELECT CAST( COALESCE( Cl_Id, '000000000' ) AS CHAR( 9 )) AS Cl_Id
, Status
, CAST( COALESCE( Placement_Category, '' ) AS CHAR( 3 )) AS Placement_Category
, CAST( COALESCE( Dob_Dt, '' ) AS CHAR( 8 )) AS Dob_Dt
, CAST( COALESCE( Ldss, '' ) AS CHAR( 3 )) AS Ldss
, CAST( COALESCE( First_Nm, '' ) AS CHAR( 20 )) AS First_Nm
, CAST( COALESCE( Last_Nm, '' ) AS CHAR( 20 )) AS Last_Nm
, CAST( COALESCE( Middle_Initial, '' ) AS CHAR( 1 )) AS Middle_Initial
, CAST( COALESCE( Street_Number, '' ) AS CHAR( 5 )) AS Street_Number
, CAST( COALESCE( Street_Nm, '' ) AS CHAR( 20 )) AS Street_Nm
, CAST( COALESCE( Additional_Street_, '' ) AS CHAR( 20 )) AS Additional_Street_Nm
, CAST( COALESCE( City_Nm, '' ) AS CHAR( 15 )) AS City_Nm
, CAST( COALESCE( State_Cd, '' ) AS CHAR( 2 )) AS State_Cd
, CAST( COALESCE( Pa_Adr_Zip5_No, '' ) AS CHAR( 5 )) AS Pa_Adr_Zip5_No
, CAST( COALESCE( Ssn_No, '000000000' ) AS CHAR( 9 )) AS Ssn_No
, CAST( COALESCE( School_Name, '' ) AS CHAR( 10 )) AS School_Name
FROM Sl_Jun2014;
EOF
Now, if you already have a text file with the script and named it (for example) "sl_jun2014.cmd", then you can do this:db2 -tvf sl_jun2014.cmd -r sl_jun2014.txt
ASKER
I have created the following script and named it as test_script.sql.
Now, i'm running this command
db2 -rsl_jun2014_out.txt <<EOF
CONNECT TO OHEP
SELECT CAST( COALESCE( Cl_Id, '000000000' ) AS CHAR( 9 )) AS Cl_Id
, Status
, CAST( COALESCE( Placement_Category, '' ) AS CHAR( 3 )) AS Placement_Category
, CAST( COALESCE( Dob_Dt, '' ) AS CHAR( 8 )) AS Dob_Dt
, CAST( COALESCE( Ldss, '' ) AS CHAR( 3 )) AS Ldss
, CAST( COALESCE( First_Nm, '' ) AS CHAR( 20 )) AS First_Nm
, CAST( COALESCE( Last_Nm, '' ) AS CHAR( 20 )) AS Last_Nm
, CAST( COALESCE( Middle_Initial, '' ) AS CHAR( 1 )) AS Middle_Initial
, CAST( COALESCE( Street_Number, '' ) AS CHAR( 5 )) AS Street_Number
, CAST( COALESCE( Street_Nm, '' ) AS CHAR( 20 )) AS Street_Nm
, CAST( COALESCE( Additional_Street_, '' ) AS CHAR( 20 )) AS Additional_Street_Nm
, CAST( COALESCE( City_Nm, '' ) AS CHAR( 15 )) AS City_Nm
, CAST( COALESCE( State_Cd, '' ) AS CHAR( 2 )) AS State_Cd
, CAST( COALESCE( Pa_Adr_Zip5_No, '' ) AS CHAR( 5 )) AS Pa_Adr_Zip5_No
, CAST( COALESCE( Ssn_No, '000000000' ) AS CHAR( 9 )) AS Ssn_No
, CAST( COALESCE( School_Name, '' ) AS CHAR( 10 )) AS School_Name
FROM Sl_Jun2014;
EOF
Now, i'm running this command
db2 -tvf test_script.sql
I'm getting these errors.DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL1024N A database connection does not exist. SQLSTATE=08003
SQL1024N A database connection does not exist. SQLSTATE=08003How do you connect to your database?
Did you source (include) the DB2 profile of the instance you are trying to use?
You may have to provide the correct CONNECT statement.
ASKER
is this command correct?
db2 -rsl_jun2014_out.txt <<EOF
Yes, it signifies db2 will execute the following commands "inline" (which you did not code) up to the "EOF".
I gave you two methods to execute your script, use the second one.
But first remove the "db2" command from your test script (line 1), and execute as follows:
I gave you two methods to execute your script, use the second one.
But first remove the "db2" command from your test script (line 1), and execute as follows:
db2 -tvf test_script.sql -routput_file.txt
ASKER
Mike, your last post executes fine.
But it is loosing the fixed length between the fields.
We need to maintain the fixed length as we generate the file.
But it is loosing the fixed length between the fields.
We need to maintain the fixed length as we generate the file.
Mmmm.....Been a long time since I used db2.
See if this one works, otherwise we may need to try the "EXPORT" command:
See if this one works, otherwise we may need to try the "EXPORT" command:
db2 -tvf test_script.sql > output_file.txt
OK found it...
1) Change your sql script as follows:
1) Change your sql script as follows:
CONNECT TO OHEP
SELECT CAST( COALESCE( Cl_Id, '000000000' ) AS CHAR( 9 )) AS Cl_Id
|| Status
|| CAST( COALESCE( Placement_Category, '' ) AS CHAR( 3 )) AS Placement_Category
|| CAST( COALESCE( Dob_Dt, '' ) AS CHAR( 8 )) AS Dob_Dt
|| CAST( COALESCE( Ldss, '' ) AS CHAR( 3 )) AS Ldss
|| CAST( COALESCE( First_Nm, '' ) AS CHAR( 20 )) AS First_Nm
|| CAST( COALESCE( Last_Nm, '' ) AS CHAR( 20 )) AS Last_Nm
|| CAST( COALESCE( Middle_Initial, '' ) AS CHAR( 1 )) AS Middle_Initial
|| CAST( COALESCE( Street_Number, '' ) AS CHAR( 5 )) AS Street_Number
|| CAST( COALESCE( Street_Nm, '' ) AS CHAR( 20 )) AS Street_Nm
|| CAST( COALESCE( Additional_Street_, '' ) AS CHAR( 20 )) AS Additional_Street_Nm
|| CAST( COALESCE( City_Nm, '' ) AS CHAR( 15 )) AS City_Nm
|| CAST( COALESCE( State_Cd, '' ) AS CHAR( 2 )) AS State_Cd
|| CAST( COALESCE( Pa_Adr_Zip5_No, '' ) AS CHAR( 5 )) AS Pa_Adr_Zip5_No
|| CAST( COALESCE( Ssn_No, '000000000' ) AS CHAR( 9 )) AS Ssn_No
|| CAST( COALESCE( School_Name, '' ) AS CHAR( 10 )) AS School_Name
FROM Sl_Jun2014;
2) Execute as follows:db2 -x +o -tf test_script.sql +o -routput_file.txt
ASKER
Mike, didn't work..wasn't creating any file with that command.
We are on AIX.
We are on AIX.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm sorry for the time lapse..
Mike, yes i have made changes to your code, but
as soon as i dump into a .txt file, the format is changed to tab delimited; not fixed length.
Mike, yes i have made changes to your code, but
as soon as i dump into a .txt file, the format is changed to tab delimited; not fixed length.
Can you post (or attach) your script and the exact command you are using?
ASKER
My apologies..i have been travelling.
Once i redirected the file to *.asc, it worked out for me.
Thanks for all your time and effort!
Once i redirected the file to *.asc, it worked out for me.
Thanks for all your time and effort!
ASKER