Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to check if field (column) exists

Hello,

I'm loading in a table that MIGHT not have a particular column. I thought I could check for it by doing an 'if exists(fieldname)' in the load script before referring to it, but when I add that logic, the script fails unceremoniously, going straight to "execution of script failed. load old data?"

I've attached a simple qvw as an example. Note it will reload ok, unless you uncomment the "if" -

I must be using the exists function incorrectly (although I wish it handled the error better). I'm trying either to make this work or find another simple way to NOT execute a portion of the script if a particular field doesn't exist.

Thanks in advance for any advice.

1 Solution

Accepted Solutions
Not applicable
Author

Hi

Try this code and it should work for you

InlineTable:
LOAD * INLINE [
Field1, Field2
1, 1
2, 3
2, 4
];
Let X = FieldNumber(Field1,'InlineTable');

if FieldNumber(Field1,'InlineTable') >= 0 then
DupeTable:
load
Field1 as NewField1,
Field2 as NewField2
resident InlineTable;
end if

Talha

View solution in original post

5 Replies
Not applicable
Author

Hi

Try this code and it should work for you

InlineTable:
LOAD * INLINE [
Field1, Field2
1, 1
2, 3
2, 4
];
Let X = FieldNumber(Field1,'InlineTable');

if FieldNumber(Field1,'InlineTable') >= 0 then
DupeTable:
load
Field1 as NewField1,
Field2 as NewField2
resident InlineTable;
end if

Talha

Not applicable
Author

THANK YOU Talha - that's exactly what I needed!

I made only a couple minor changes (I used the variable in the condition, and made condition "greater than" instead of "greater than or equal to" zero):

InlineTable:
LOAD * INLINE [
Field Name1, Field2
1, 1
2, 3
2, 4
];

Let X = FieldNumber('Field Name1','InlineTable');

if X>0 then
DupeTable:
load
[Field Name1] as NewField1,
Field2 as NewField2
resident InlineTable;
end if

Thanks again

rakeshshah
Partner - Creator
Partner - Creator

Hi Monty/Anyone,

Thanks for the answer on this - is there a way to do this more dynamically inside the load statement. The reason I ask is we have a 60+ QVD tables (weekly snapshots) and I don't want to load each QVD/table twice.

Thanks

Rakesh

megabyte23
Contributor III
Contributor III

This is old, but you could do it in a subroutine.

Sakura
Creator
Creator

Could you please provide a small example of using subroutine? 

I have to check if the column exists from the source table then load the column else load some text in the column.