企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 在 SAP ABAP 中打开 SQL &本机 SQL > 原文: [https://www.guru99.com/native-open-sql.html](https://www.guru99.com/native-open-sql.html) The goal of this tutorial is not to teach you [SQL](/sql.html) or database concepts but to  introduce you to the SQL diversity in ABAP 在 ABAP / 4 编程语言中,使用两种类型的 SQL。 1. 原生 SQL 2. OPEN SQL。 Open SQL allows you to access the database tables declared in the ABAP dictionary regardless of the database platform that the R/3 system is using. 本机 SQL 允许您在 ABAP / 4 程序中使用特定于数据库的 SQL 语句。 这意味着您可以使用不是由 ABAP 词典管理的数据库表,因此可以集成不属于 R / 3 系统的数据。 Open SQL 由一组 ABAP 语句组成,这些语句在 R / 3 系统中的中央数据库上执行操作。 操作结果和任何错误消息均与所使用的数据库系统无关。 因此,Open SQL 为 SAP 支持的所有数据库系统提供了统一的语法和语义。 无论使用什么数据库系统,仅使用 Open SQL 语句的 ABAP 程序都可以在任何 R / 3 系统中工作。 Open SQL 语句只能与在 ABAP 词典中创建的数据库表一起使用。 ## 基本的开放式 SQL 命令 * 选择 * 插 * 更新 * 修改 * 删除 * 打开光标,关闭光标,关闭光标 Example ``` TABLES SBOOK. DATA C TYPE CURSOR, WA LIKE SBOOK. OPEN CURSOR C FOR SELECT * FROM SBOOK WHERE CARRID = 'LH ' AND CONNID = '0400' AND FLDATE = '19950228' ORDER BY PRIMARY KEY. DO. FETCH NEXT CURSOR C INTO WA. IF SY-SUBRC <> 0. CLOSE CURSOR C. EXIT. ENDIF. WRITE: / WA-BOOKID, WA-CUSTOMID, WA-CUSTTYPE, WA-SMOKER, WA-LUGGWEIGHT, WA-WUNIT, WA-INVOICE. ENDDO. ``` 输出 1995 年 2 月 28 日汉莎航空 0400 航班的乘客清单: ## **打开 SQL 返回码** All Open SQL statements fill the following two system fields with return codes. **SY-SUBRC** 在每个 Open SQL 语句之后,如果操作成功,则系统字段 SY-SUBRC 包含值 0,否则返回非 0 的值。 **SY-DBCNT** 在执行 Open SQL 语句后,系统字段 SY-DBCNT 包含已处理的数据库行数。 ## 本机 SQL As already mentioned, Native SQL allows you to use database-specific SQL statements in an ABAP program. 要使用本机 SQL 语句,必须在其前面加上 EXEC SQL 语句,然后在其后面加上 ENDEXEC 语句。 句法 ``` EXEC SQL [PERFORMING <form>]. <Native SQL statement> ENDEXEC. ``` There is no period after Native SQL statements. Furthermore, using inverted commas (") or an asterisk (*) at the beginning of a line in a native SQL statement does not introduce a comment as it would in normal ABAP syntax. You need to know whether table and field names are case-sensitive in your chosen database. 在本机 SQL 语句中,使用主机变量在数据库表和 ABAP 程序之间传输数据。 它们在 ABAP 程序中声明,并在本机 SQL 语句中以冒号(:)开头。 您可以将基本结构用作宿主变量。 例外地,将 INTO 子句中的结构视为其所有字段均单独列出。 与 Open SQL 中一样,在 ENDEXEC 语句之后,SY-DBCNT 包含已处理的行数。 在几乎所有情况下,ENDEXEC 语句后 SY-SUBRC 都包含值 0。 ## 开放式 SQL-性能规则 To improve the performance of the SQL and in turn of the ABAP program, one should take care of the following rules- **保持结果集较小** * 使用 where 子句 * 如果仅需要数据库中的一条记录,请尽可能使用 SELECT SINGLE。 **Minimize the Amount of Data Transferred** * 限制行数 * 如果表中仅需要某些字段,请使用 SELECT < field1 > < field2 > INTO ...语句 * 限制列数 * 使用聚合函数 **Minimize the Number of Data Transfers** * 避免嵌套选择循环 * 另一种选择是使用 SELECT .. FOR ALL ENTRIES 语句。 与在内部表的 LOOP 期间执行大量 SELECT 或 SELECT SINGLE 语句相比,该语句通常效率更高。 * 使用字典视图 * 在 FROM 子句中使用联接 * 在 where 子句中使用子查询 **Minimize the Search Overhead** * 在 where 子句中使用索引字段 * 访问数据库时,请始终确保使用了正确的索引。 **Reduce the Database Load** * 正在缓冲 * 逻辑数据库 * 避免重复访问数据库 **Using Internal Tables to Buffer Records** * 为了避免多次执行相同的 SELECT(因此有重复的选择),可以使用 HASHED 类型的内部表来提高性能。