SAP HANA SQL:10 分钟内学习基本语句

⚡ 智能摘要

SAP HANA SQL is the main database language of the platform. It handles schema definition, data manipulation, access control, and session management, following the standard that makes SQL portable across relational databases.

  • 🗣️ 核心角色: SQL is the primary language for communicating with SAP HANA, as it is for other relational systems.
  • ????️ Schema Work: CREATE SCHEMA establishes the namespace that owns tables, views, and procedures.
  • 📋 DML Statements: SELECT, INSERT, and UPDATE read and change the rows held in a table.
  • 🧱 DDL Statements: CREATE, ALTER, and DROP define and reshape the objects themselves.
  • 🔐 DCL Statements: GRANT and REVOKE control which user may perform which operation.
  • 💬 Comment Styles: Double hyphen marks a single line comment and slash asterisk encloses a multiple line comment.
  • ⚙️ 管理: System, session, and transaction management are all driven through SQL statements.

SAP HANA SQL

大多数 RDBMS 数据库使用 SQL 作为数据库语言,它受欢迎的原因是——功能强大、独立于供应商并且标准化。 SAP HANA 还支持 SQL。

In SAP HANA,SQL是主要的数据库语言。

什么是 SAP HANA SQL?

SQL 代表结构化查询语言。它是一种与关系数据库进行通信的标准语言,例如 Oracle, MySQL 等等。SQL用于存储、检索和修改数据库中的数据。

通过使用 SQL SAP HANA,我们可以执行以下工作-

  • 模式定义和使用(CREATE SCHEMA)。
  • DML 语句(SELECT、UPDATE、INSERT)。
  • DDL Statement (CREATE, ALTER, DROP)
  • DCL Statement (GRANT, REVOKE)
  • 系统管理
  • 会话管理
  • 交易管理

Statements are executed from the SQL console of SAP HANA 工作室, from a command line client, or from an application connecting over JDBC or ODBC. The language itself is identical in each case.

SQL Statement Categories in SAP HANA

Every statement belongs to one of a small number of families. Knowing which family a statement sits in predicts whether it can be rolled back and what authorisation it needs.

类别 姓名 Typical statements 目的
DDL 数据定义语言 CREATE, ALTER, DROP, RENAME Defines and changes objects such as schemas, tables, and views
DML 数据处理语言 SELECT, INSERT, UPDATE, DELETE, UPSERT Reads and changes the rows held inside objects
DCL 数据控制语言 GRANT, REVOKE Controls which user or role may perform which action
TCL集团 交易控制语言 COMMIT, ROLLBACK, SAVEPOINT Confirms or discards a unit of work
时间 多场会议管理 SET SCHEMA, CONNECT, SET TRANSACTION Configures the current connection
系统 系统管理 ALTER SYSTEM, monitoring views Administers the database instance itself

UPSERT is worth noting because it does not exist in every dialect. It inserts a row when the key is new and updates it when the key already exists, which removes a common two statement pattern.

SQL 中的注释

我们可以添加注释来提高 SQL 语句的可读性和可维护性。注释可以通过两种方式添加到 SQL 中:

  • 单行注释 – Double Hyphens “–“. This is one line comment.
  • Multiple Line Comment – “/* */”.

SQL 分析器会忽略所有注释文本。

-- This is a single line comment
SELECT ELEMENT FROM DHK_SCHEMA.TABLE1;

/* This is a multiple line comment.
   Everything between the markers is ignored
   by the SQL parser. */
SELECT COUNT(*) FROM DHK_SCHEMA.TABLE1;

Basic SQL Syntax Examples in SAP HANA

The statements below cover the everyday cycle of creating a schema, defining a table, loading rows, and reading them back. They run unchanged in the SQL console.

1) Create a schema. A schema owns the objects created inside it and is the first thing a new project needs.

CREATE SCHEMA DHK_SCHEMA;
SET SCHEMA DHK_SCHEMA;

2) Create a column table. Column store is the default and the reason for HANA analytical speed. Row store is chosen only for small, write heavy tables.

CREATE COLUMN TABLE DHK_SCHEMA.EMPLOYEE (
  EMP_ID    INTEGER NOT NULL,
  EMP_NAME  NVARCHAR(100),
  HIRE_DATE DATE,
  SALARY    DECIMAL(10,2),
  PRIMARY KEY (EMP_ID)
);

3) Insert and update rows. INSERT adds a row and UPDATE changes existing ones. Always pair an UPDATE with a WHERE clause unless every row really should change.

INSERT INTO DHK_SCHEMA.EMPLOYEE
  VALUES (1, 'Anita Sharma', '2026-01-15', 55000.00);

UPDATE DHK_SCHEMA.EMPLOYEE
  SET SALARY = 60000.00
  WHERE EMP_ID = 1;

4) Query the data. SELECT supports the familiar WHERE, GROUP BY, HAVING, and ORDER BY clauses.

SELECT EMP_NAME, SALARY
  FROM DHK_SCHEMA.EMPLOYEE
  WHERE SALARY > 50000
  ORDER BY SALARY DESC;

5) Grant access and commit. DCL controls who may read the table, and COMMIT makes the work permanent.

GRANT SELECT ON DHK_SCHEMA.EMPLOYEE TO REPORT_USER;
COMMIT;

Field level detail on each column definition is covered in SAP HANA data types, and the symbols used in the WHERE clause in SAP HANA operators.

SAP HANA SQL vs Standard SQL

SAP HANA follows the SQL standard closely, which is why an experienced developer is productive quickly. The differences that matter are additions rather than departures.

区域 标准 SQL SAP HANA SQL
Table storage Row oriented by default Column store by default, row store optional
Insert or update in one statement MERGE, where supported UPSERT, simpler syntax for the same result
Result set limit Dialect specific LIMIT with OFFSET, plus TOP
Procedural extension 供应商特定 SQLScript, with table variables and parallel execution
全文搜索 Usually an add on Built in through TEXT and SHORTTEXT columns
区分大小写 可变 Undelimited identifiers are upper cased, delimited ones keep their case

The identifier rule causes the most surprises. Writing a table name in lower case without double quotes creates it in upper case, and later selecting it inside double quotes in lower case then fails to find it. The rules are set out in the data types and identifiers tutorial, with the wider platform covered in the SAP HANA tutorial 系列丛书中。

常见问题

SQL handles single statements. SQLScript adds procedural logic such as variables, loops, and table variables, letting several steps run inside the database instead of in the application layer.

Column tables for almost everything, because compression and analytical reads are far faster. Row tables suit small configuration tables read entirely, record by record, on every access.

Yes. Given the schema, language models draft correct SELECT statements from a written request. Review joins and filters carefully, since a plausible query can still answer the wrong question.

Yes. AI reads the execution plan and suggests rewrites, better join order, or missing filters. Measure the result, because a suggestion that helps one data volume can hurt another.

No. The parser discards them before execution, so comments cost nothing at runtime. They are the cheapest way to make a long analytical statement maintainable.

总结一下这篇文章: