我试图让我的脚本在QlikView中重新加载但没有成功。我在最近生成的QVD文件中添加了一个新字段(名为Litres
)。但是,我的旧QVD文件没有此Litres
字段,因此导致脚本错误输出“找不到字段”错误。
我正在尝试执行以下操作:
Litres
字段存在,则将数据加载到QlikView。上述情况不应导致错误。
我当前的脚本如下:
/* get all sales csvs */
sales:
load 2014 as Year
AutoGenerate 0;
set FilePath = ..\..\SourceData\qv-sales*.csv;
for each File in filelist('$(FilePath)')
/* load qvd file if it is newer than csv file */
temptable1:
first 1 LOAD
*
FROM $(File)
(txt, codepage is 1252, embedded labels, delimiter is ',', msq);
IF FieldNumber('Litres' , 'temptable1') <> null then
SET "Litres";
SET "Litres LY" ;
SET dyncode2 = P-Value;
SET dyncode3 = O-P-Value;
SET dyncode4 = P-Value LY;
SET dyncode5 = O-P-Value LY;
SET dyncode6 = P-Qty;
SET dyncode7 = P-O-Qty;
SET dyncode8 = P-Qty LY;
SET dyncode9 = P-O-Qty LY;
ENDIF;
DROP TABLE temptable1;
let qvdFile = replace('$(File)','csv','qvd');
if (QvdCreateTime('$(qvdFile)') >= FileTime('$(File)')) then
/* load qvd file if it is newer than csv file */
sales:
Concatenate (sales) load
"GP",
"O-GP",
"Litres",
"Litres LY",
"GP LY",
"O-GP LY",
$(dyncode2) as "P-Value",
$(dyncode3) as "O-P-Value",
$(dyncode4) as "P-Value LY",
$(dyncode5) as "O-P-Value LY",
$(dyncode6) as "P-Qty",
$(dyncode7) as "P-O-Qty",
$(dyncode8) as "P-Qty LY",
$(dyncode9) as "P-O-Qty LY" from $(qvdFile) (qvd);
else
/* create temp store key1 & key2 are manually handled synthetic keys */
/* Dummy field is to prevent autoconcatenation problems. */
temptable:
noconcatenate Load
"GP",
"O-GP",
"Litres" ,
"Litres LY",
"GP LY",
"O-GP LY",
$(dyncode2) as "P-Value",
$(dyncode3) as "O-P-Value",
$(dyncode4) as "P-Value LY",
$(dyncode5) as "O-P-Value LY",
$(dyncode6) as "P-Qty",
$(dyncode7) as "P-O-Qty",
$(dyncode8) as "P-Qty LY",
$(dyncode9) as "P-O-Qty LY",
autonumber(1) as dummy1
FROM $(File) (ansi, txt, delimiter is ',', embedded labels);
/* create qvd file from temp table */
if (ScriptErrorCount = 0) then
Store temptable into $(qvdFile);
endif
sales:
concatenate("sales") load
"Litres",
"Litres LY",
"GP LY",
"O-GP LY"
"P-Value",
"O-P-Value",
"P-Value LY",
"O-P-Value LY",
"P-Qty",
"P-O-Qty",
"P-Qty LY",
"P-O-Qty LY"
resident temptable;
/* drop temp table */
DROP TABLE temptable;
endif
next File
提前致谢!
答案 0 :(得分:1)
您可以使用FieldNumber
函数来确定表中是否存在字段。例如,FieldNumber('MyField', 'MyTable')
会在MyField
内返回MyTable
的位置。如果MyField
中不存在MyTable
,则函数返回零。
您可以通过将QVD中的所有字段加载到临时表中,然后检查此表是否包含该字段来调整此优势。如果是,您可以继续加载。如果没有,您只需将该字段设置为零。
我已经调整了您的脚本并为此目的插入了一个IF:
/* get all sales csvs */
sales:
load 2014 as Year
AutoGenerate 0;
set FilePath = ..\..\SourceData\qv-sales*.csv;
for each File in filelist('$(FilePath)')
/* load qvd file if it is newer than csv file */
temptable1:
first 1 LOAD
*
FROM $(File)
(txt, codepage is 1252, embedded labels, delimiter is ',', msq);
IF FieldNumber('Litres' , 'temptable1') <> null then
SET "Litres";
SET "Litres LY" ;
SET dyncode2 = P-Value;
SET dyncode3 = O-P-Value;
SET dyncode4 = P-Value LY;
SET dyncode5 = O-P-Value LY;
SET dyncode6 = P-Qty;
SET dyncode7 = P-O-Qty;
SET dyncode8 = P-Qty LY;
SET dyncode9 = P-O-Qty LY;
ENDIF
DROP TABLE temptable1;
let qvdFile = replace('$(File)','csv','qvd');
if (QvdCreateTime('$(qvdFile)') >= FileTime('$(File)')) then
/* load qvd file if it is newer than csv file */
fieldcheck:
FIRST 1
NOCONCATENATE
LOAD
*
FROM $(qvdFile) (qvd);
if FieldNumber('Litres','fieldcheck') = 0 then
drop table fieldcheck;
sales:
Concatenate (sales) load
"GP",
"O-GP",
0 as "Litres",
0 as "Litres LY",
"GP LY",
"O-GP LY",
$(dyncode2) as "P-Value",
$(dyncode3) as "O-P-Value",
$(dyncode4) as "P-Value LY",
$(dyncode5) as "O-P-Value LY",
$(dyncode6) as "P-Qty",
$(dyncode7) as "P-O-Qty",
$(dyncode8) as "P-Qty LY",
$(dyncode9) as "P-O-Qty LY" from $(qvdFile) (qvd);
else
drop table fieldcheck;
sales:
Concatenate (sales) load
"GP",
"O-GP",
"Litres",
"Litres LY",
"GP LY",
"O-GP LY",
$(dyncode2) as "P-Value",
$(dyncode3) as "O-P-Value",
$(dyncode4) as "P-Value LY",
$(dyncode5) as "O-P-Value LY",
$(dyncode6) as "P-Qty",
$(dyncode7) as "P-O-Qty",
$(dyncode8) as "P-Qty LY",
$(dyncode9) as "P-O-Qty LY" from $(qvdFile) (qvd);
endif
else
/* create temp store key1 & key2 are manually handled synthetic keys */
/* Dummy field is to prevent autoconcatenation problems. */
temptable:
noconcatenate Load
"GP",
"O-GP",
"Litres" ,
"Litres LY",
"GP LY",
"O-GP LY",
$(dyncode2) as "P-Value",
$(dyncode3) as "O-P-Value",
$(dyncode4) as "P-Value LY",
$(dyncode5) as "O-P-Value LY",
$(dyncode6) as "P-Qty",
$(dyncode7) as "P-O-Qty",
$(dyncode8) as "P-Qty LY",
$(dyncode9) as "P-O-Qty LY",
autonumber(1) as dummy1
FROM $(File) (ansi, txt, delimiter is ',', embedded labels);
/* create qvd file from temp table */
if (ScriptErrorCount = 0) then
Store temptable into $(qvdFile);
endif
sales:
concatenate("sales") load
"Litres",
"Litres LY",
"GP LY",
"O-GP LY"
"P-Value",
"O-P-Value",
"P-Value LY",
"O-P-Value LY",
"P-Qty",
"P-O-Qty",
"P-Qty LY",
"P-O-Qty LY"
resident temptable;
/* drop temp table */
DROP TABLE temptable;
endif
next File
答案 1 :(得分:0)
如果我的理解是正确的,则需要重新生成QVD,因为您添加了先前字段中不存在的新字段,因此您需要从源(txt)重新创建它们并进行存储。 一种更简单的方法是利用以下事实:当您对表执行forced concatenate时,会生成带有null的缺失字段,我们可以将其与函数alt一起使用以转换空为0。 举个例子:
/* get all sales csvs */
sales:
load 2014 as Year
AutoGenerate 0;
set FilePath = ..\..\SourceData\qv-sales*.csv;
for each File in filelist('$(FilePath)')
let qvdFile = replace('$(File)','csv','qvd');
//this generate the field Litres, exists or not
tmp: noconcatenate load null() as Litres autogenerate(0);
if (QvdCreateTime('$(qvdFile)') >= FileTime('$(File)')) then
/* load qvd file if it is newer than csv file */
concatenate(tmp) LOAD * FROM $(qvdFile) (qvd);
else
concatenate(tmp) LOAD * FROM $(File)
(txt, codepage is 1252, embedded labels, delimiter is ',', msq);
endif;
tmp_sales:
noconcatenate
load
alt(Litres,0) as Litres,
"Litres LY",
"GP LY",
"O-GP LY"
"P-Value",
"O-P-Value",
"P-Value LY",
"O-P-Value LY",
"P-Qty",
"P-O-Qty",
"P-Qty LY",
"P-O-Qty LY"
resident tmp;
drop table tmp;
//if it was newer that the csv we store it
if (QvdCreateTime('$(qvdFile)') >= FileTime('$(File)')) then
Store tmp_sales into $(qvdFile);
endif
concatenate(sales) load * resident tmp_sales;
drop table tmp_sales;
next File