# Exists 指令碼函數

Exists() 會判定特定欄位值是否已載入資料載入指令碼的欄位中。該函數會傳回 TRUE 或 FALSE﹐因此可以在 LOAD 陳述式或 IF 陳述式的 where 子句中使用。

資訊備註您也可以使用 Not Exists() 以判定欄位值是否未載入，但建議您謹慎使用 where 子句中的 Not Exists()。Exists() 函數可在目前表格中測試先前載入的表格和先前載入的值。因此，只有首次出現時才會載入。第二次出現時，該值已經載入。請參閱範例瞭解更多資訊。

語法：

Exists(field\_name [, expr])

傳回的資料類型： 布林值

引數：

引數

| 引數 | 描述 |
| --- | --- |
| field\_name | 想要搜尋值的欄位名稱。您可以使用不含引號的明確欄位名稱，<br><br>欄位必須已由指令碼載入。這代表，您無法參考在指令碼中進一步向下的子句中載入的欄位。 |
| expr | 您想要檢查是否存在的值。您可以使用明確的值或參考目前 LOAD 陳述式中一個或數個欄位的運算式。<br><br>資訊備註您無法參考未納入目前 LOAD 陳述式的欄位。<br><br>此引數為選擇性。若您省略，函數將會檢查目前記錄中 field\_name 的值是否已存在。 |

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

#### 範例與結果：

範例 1

`Exists 
 (Employee)`

如果目前記錄中欄位 Employee 的值已經存在於任何先前讀取的記錄 (包含該欄位) 中，則會傳回 -1 (True)。

陳述式 `Exists (Employee, Employee)` 與 `Exists (Employee)` 相當。

範例 2

`Exists(Employee, 
 'Bill')`

如果欄位值 'Bill' 被發現位於欄位 Employee 的目前內容中，則會傳回 -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 欄位中。因此，檢查第二行時，值已經存在。

結果

| 員工 | 地址 |
| --- | --- |
| 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 的所有值，已變更兩件事：

* 插入了前置載入到 Employees 表格，其中 Employee 已重新命名為 Name。

  `
  Load Employee As Name;`

* Citizens 中的 Where 條件已變更為：

  `not Exists (Name, Employee).`

這會為 Name 和 Employee 建立欄位。檢查含有 Lucy 的第二列時，這仍然不會存在於 Name。

結果

| 員工 | 地址 |
| --- | --- |
| Mary | London |
| Lucy | Madrid |
| Lucy | Paris |