CSV文件中的空值

时间:2018-04-11 06:16:06

标签: oracle csv plsql

我正在尝试创建CSV文件以生成数据,我希望将空值保存为列中的字符串,但是根据附加的图片屏幕截图,它无法正确拟合

Screenshot

我的程序:

CREATE OR REPLACE procedure ABLEA_NEW.DATA_BAH
as
fHandle UTL_FILE.FILE_TYPE;
err  varchar2(200);
rec  varchar2(300);
cursor data1 is SELECT A."CUSTOMERNAME", A."CUST_ID_N",A."NATIONALID",R."NATIONALITY",A."LOS_EXISTING_CUST_C",A."CIF_NO",A."PASSPORTNO",A."MONTHLY_INCOME",
    A."DOB",A."SEX",A.PAN_NO,C.ADDRESSID,C.PHONE1,C.ADDRESS1,X.AGREEMENTNO,A.FNAME, R.COMP_NAME,R.ACCOTYPE
    from NBFC_CUSTOMER_M R,ABCAS_NEW.NBFC_CUSTOMER_M A,LEA_GUARANTOR_HIRER_DTL B ,ABCAS_NEW.NBFC_ADDRESS_M c,LEA_AGREEMENT_DTL X WHERE A.CUST_ID_N = B.GUARANTOR_HIRER_ID and  C.BPID = B.GUARANTOR_HIRER_ID AND X.AGREEMENTID=B.APPLID AND A.CUST_ID_N=R.CUSTOMERID;


all_data1 data1%rowtype;
begin
fHandle := UTL_FILE.FOPEN('BAHDB', 'DATA_BAH', 'W');
open data1;
loop
fetch data1 into all_data1;
exit when data1%notfound;
rec:=nvl(rpad(all_data1.CUSTOMERNAME,30,' '),'  NotFound  ')||nvl(rpad(all_data1.NATIONALITY,20,' '),'  NOT FOUND  ')||nvl(rpad(all_data1.CUST_ID_N,10,' '),'  NotFound  ')||nvl(rpad(all_data1.NATIONALID,3,' '),'  NotFound  ')||nvl(rpad(all_data1.NATIONALITY,10,''),'  NotFound  ')||
nvl(rpad(all_data1.LOS_EXISTING_CUST_C,7,' '),'  NotFound  ')||nvl(rpad(all_data1.CIF_NO,15,' '),'  NotFound  ')||nvl(rpad(all_data1.PASSPORTNO,15,''),'  NotFound  ')||nvl(rpad(all_data1.MONTHLY_INCOME,8,' '),'  NotFound  ')||nvl(rpad(all_data1.DOB,15,''),'  NotFound  ')||
nvl(rpad(all_data1.DOB,15,' '),'  NotFound  ')|| nvl(rpad(all_data1.ADDRESSID,5,' '),'  NotFound  ')||nvl(rpad(all_data1.AGREEMENTNO,25,' '),'  NotFound  ')||nvl(rpad(all_data1.FNAME,7,' '),'  NotFound  ')||nvl(rpad(all_data1.COMP_NAME,15,' '),'  NotFound  ') ||nvl(rpad(all_data1.ACCOTYPE,15,' '),'  NotFound  ');
UTL_FILE.PUTF(fHandle,rec,' ');
UTL_FILE.PUTF(fHandle, '\n');

end loop;
UTL_FILE.FCLOSE(fHandle);
close data1;
end;
/ 

1 个答案:

答案 0 :(得分:2)

啊,您使用的是CSV这个词,但是您没有生成以逗号分隔的值。相反,您正在尝试生成一个文件,其中属性由位置对齐。

所以你的问题是你的NULL替换值不适合为实际值分配的空间。考虑一下这个:

nvl(rpad(all_data1.CUST_ID_N,10,' '),'  NotFound  ')

填充all_data1.CUST_ID_N时,您将结果填充为十个字符,但如果为空,则替换长<十二个字符的字符串。这将使后续值的偏移量减去两个字符。更安全的方法是:

rpad(nvl(all_data1.CUST_ID_N,'NotFound'),10,' ')

这样,无论源数据如何,所有属性都会对齐。

顺便说一句,我认为这是发出空值信号的可怕方法。数据导出工具具有处理实际空值的内置功能,而加载此文件则需要对NotFound进行定制处理。这对于应该是数字的列(例如MONTHLY_INCOME)尤其烦人,但必须作为字符串加载并转换为数字数据类型(在处理NotFound之后)。