> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# try_to_file

> 根据 STRUCT 表达式构造 FILE 值，字段值非法的行返回 NULL。

**实验性功能**

[关于使用实验性功能的建议](/documents/cn/4.2/maturity/features.md)

使用与 [to\_file](/documents/cn/4.2/sql-reference/sql-functions/file-functions/to_file.md) 相同的字段、类型和校验规则构造临时 FILE。仅将字段值非法的行置为 NULL，其他行不受影响。

## 语法

```sql theme={null}
FILE try_to_file(STRUCT struct_expr)
```

## 参数

`struct_expr`：与 [to\_file](/documents/cn/4.2/sql-reference/sql-functions/file-functions/to_file.md) 相同。

## 返回值

返回 FILE 值。如果转换 FILE 失败，则直接返回 NULL。

## 使用说明

* 未知字段、重复字段、不支持的类型以及错误参数数量仍在分析阶段报错。参数表达式求值错误、内部错误和资源错误不会转成 NULL。

## 示例

### 示例 1：合法输入

```sql theme={null}
SELECT try_to_file(named_struct('uri', 'a.jpg', 'size', 12)) AS f;
```

```plaintext theme={null}
+-------------------------------------------------------------------------------------------+
| f                                                                                         |
+-------------------------------------------------------------------------------------------+
| {"uri":"a.jpg","offset":null,"size":12,"content_type":null,"checksum":null,"inline":null} |
+-------------------------------------------------------------------------------------------+
```

### 示例 2：非法行返回 NULL

```sql theme={null}
SELECT
    try_to_file(named_struct('uri', 'a.jpg', 'size', -1)) IS NULL AS negative_size,
    try_to_file(named_struct('uri', NULL, 'inline', NULL)) IS NULL AS neither_set,
    try_to_file(named_struct('uri', 'a.jpg', 'inline', to_binary('61', 'hex'))) IS NULL AS both_set;
```

```plaintext theme={null}
+---------------+-------------+----------+
| negative_size | neither_set | both_set |
+---------------+-------------+----------+
| 1             | 1           | 1        |
+---------------+-------------+----------+
```

## keyword

TRY\_TO\_FILE, FILE


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.