# Exists - 脚本函数

Exists() 用于确定是否已经将特定字段值加载到数据加载脚本中的字段。此函数用于返回 TRUE 或 FALSE，这样它可以用于 LOAD 语句或 IF 语句中的 where 子句。

信息注释您也可使用 Not Exists() 来确定是否尚未加载字段值，但是如果要在 where 子句中使用 Not Exists()，建议您小心。Exists() 函数在当前表格中测试之前加载的表格和之前加载的值。因此，仅加载第一次出现的值。如果遇到第二次出现的值，值已经被加载。有关更多信息，请查看示例。

语法：

Exists(field\_name [, expr])

返回数据类型： 布尔值

参数：

参数

| 参数 | 说明 |
| --- | --- |
| field\_name | 您希望在其中搜索值的字段的名称。您可使用没有引号的确切字段名称。<br><br>字段必须已经由脚本加载。这意味着您无法在脚本中进一步往下引用在子句中加载的字段。 |
| expr | 您希望检查值是否存在。您可使用引用当前加载语句中一个或数个字段的确切值或表达式。<br><br>信息注释您无法引用未包含在当前加载语句中的字段。<br><br>该参数为可选。如果您忽略它，函数将检查当前记录中的 field\_name 的值是否已存在。 |

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/InterRecordFunctions/Exists.htm)

#### 示例和结果：

示例 1

`Exists 
 (Employee)`

如果当前记录中的字段值 Employee 已存在于任何以前已读入的包含该字段的记录，则返回 -1 (True)。

语句 `Exists (Employee, Employee)` 和 `Exists (Employee)` 功能相同。

示例 2

`Exists(Employee, 
 'Bill')`

如果在字段 Employee（员工）的当前内容中发现字段值 'Bill'，则返回 -1 (True)。

示例 3

```
Employees:
LOAD * inline [
Employee|ID|Salary
Bill|001|20000
John|002|30000
Steve|003|35000
] (delimiter is '|');

Citizens:
Load * inline [
Employee|Address
Bill|New York
Mary|London
Steve|Chicago
Lucy|Madrid
Lucy|Paris
John|Miami
] (delimiter is '|') where Exists (Employee);

Drop Tables Employees;
```

由此得到表格，您可借助维度Employee 和 Address 在表格可视化中使用该表格。

where 子句：`where Exists (Employee)`，是指只能从表格 Citizens（市民）将同时位于 Employees（员工）中的姓名加载到新表格。Drop 语句删除表格 Employees（员工）以避免混淆。

结果

|  |  |
| --- | --- |
| Employee | Address |
| Bill | New York |
| John | Miami |
| Steve | Chicago |

示例 4：

```
Employees:
Load * inline [ 
Employee|ID|Salary
Bill|001|20000
John|002|30000
Steve|003|35000
] (delimiter is '|');
 
Citizens:
Load * inline [
Employee|Address
Bill|New York
Mary|London
Steve|Chicago
Lucy|Madrid
Lucy|Paris
John|Miami
] (delimiter is '|') where not Exists (Employee);
 
Drop Tables Employees;
```

where 子句包括 not: `where not Exists (Employee)`。

这意味着只能从表格 Citizens（市民）将不在 Employees（员工）中的姓名加载到新表格。

请注意在 Citizens（市民）中对于 Lucy 有两个值，但是在结果表格中仅包含了一个。当您加载带值 Lucy 的第一行时，其包含在 Employee 字段中。因此，当检查第二行时，已存在值。

结果

| Employee | Address |
| --- | --- |
| Mary | London |
| Lucy | Madrid |

示例 5

该示例示出如何加载所有值。

```
Employees:
Load Employee As Name;
LOAD * inline [
Employee|ID|Salary
Bill|001|20000
John|002|30000
Steve|003|35000
] (delimiter is '|');
 
Citizens:
Load * inline [
Employee|Address
Bill|New York
Mary|London
Steve|Chicago
Lucy|Madrid
Lucy|Paris
John|Miami
] (delimiter is '|') where not Exists (Name, Employee);
 
Drop Tables Employees;
```

要得到 Lucy 的所有值，您需要进行两项更改：

* 在 Employee 重命名为 Name 的位置插入了 Employees 表的前置 Load。

  `
  Load Employee As Name;`

* 在 Citizens 中将 Where 条件更改为：

  `not Exists (Name, Employee).`

这将为 Name 和 Employee 创建字段。如果检查带 Lucy 的第二行，它仍未存在于 Name（名称）中。

结果

| Employee | Address |
| --- | --- |
| Mary | London |
| Lucy | Madrid |
| Lucy | Paris |