IDENTIFIER-Klausel

Gilt für:Häkchen gesetzt ja Databricks SQL durch Häkchen mit „Ja“ markiert Databricks Runtime 13.3 LTS und höher

Die IDENTIFIER Klausel ermöglicht die SQL-Injection-sichere Parametrisierung von Bezeichnern in SQL-Anweisungen.

  • Gilt für:check marked yes Databricks SQL check marked yes Databricks Runtime 18.0 und höher

    Die Klausel kann anstelle eines Objektnamens oder Bezeichners innerhalb eines mehrteiligen Namens verwendet werden, solange die Argumente nur aus einer Zeichenfolgenliteral - oder Zeichenfolgenparametermarkierung bestehen. Dazu gehören verkettete Zeichenfolgen wie: 'myschema' :table 'mycolumn' oder :schema :table :column.

    Im SQL-Skripting umfasst das:

  • Gilt für:Häkchen gesetzt ja Databricks SQL durch Häkchen mit „Ja“ markiert Databricks Runtime 13.3 LTS und höher

    Die IDENTIFIER Klausel interpretiert eine konstante Zeichenfolge wie 'myschema' || :table || 'mycolumn' oder :schema || :table || :column als ein:

    • Relationsname (Tabelle oder Sicht)
    • Funktionsname
    • Spaltenname
    • Feldname
    • Schemaname
    • Katalogname

    Die IDENTIFIER Klausel ist auf die folgenden Anweisungen beschränkt:

    • Der Objektname einer Tabelle, Ansicht oder Funktion in einer CREATE-, ALTER-, DROP- oder UNDROP-Anweisung.
    • Der Name der Zieltabelle einer MERGE-, UPDATE DELETE-, INSERTCOPY INTO-Anweisung.
    • Das Ziel einer SHOW- oder DESCRIBE-Anweisung.
    • VERWENDUNG eines Schemas oder Katalogs
    • Ein Funktionsaufruf
    • Eine Spalte, Tabelle oder Ansicht, auf die in einer Abfrage verwiesen wird. Hierzu gehören auch Abfragen, die in eine DDL- oder DML-Anweisung eingebettet sind.

Hinweis

Wenn die IDENTIFIER Klausel nicht unterstützt wird und die SQL-Einfügung kein Problem darstellt, können Sie stattdessen verwenden EXECUTE IMMEDIATE . Beispiel: EXECUTE IMMEDIATE 'CREATE TABLE ' || :tab || '(' || :col || 'INT)';

Syntax

IDENTIFIER ( strLiteral )

IDENTIFIER ( strExpr )

Parameter

  • strLiteral: Ein STRING Literal, das in der Regel aus einem oder mehreren Zeichenfolgenparametermarkern und Literalkomponenten besteht, die zusammengekettet sind;
  • strExpr: Ein konstanter STRING-Ausdruck, der in der Regel einen oder mehrere Parametermarker enthält. Ab Databricks Runtime 18.0 ist diese Notation veraltet.

Beispiele

Scala

// Creation of a table using parameter marker.
spark.sql("CREATE TABLE IDENTIFIER(:mytab)(c1 INT)", args = Map("mytab" -> "tab1"))

// Altering a table with a fixed schema and a parameterized table name.
spark.sql("ALTER TABLE IDENTIFIER('default.' || :mytab) ADD COLUMN c2 INT", args = Map("mytab" -> "tab1"))

// Altering a table with a fixed schema and a parameterized table name in :re[DBR] 18.0 and above.
spark.sql("ALTER TABLE IDENTIFIER('default.' :mytab) ADD COLUMN c2 INT", args = Map("mytab" -> "tab1"))

// Dropping a table with separate schema and table parameters.
spark.sql("DROP TABLE IDENTIFIER(:myschema || '.' || :mytab)", args = Map("myschema" -> "default", "mytab" -> "tab1"))

// Dropping a table with separate schema and table parameters in :re[DBR] 18.0 and above.
spark.sql("DROP TABLE IDENTIFIER(:myschema '.' :mytab)", args = Map("myschema" -> "default", "mytab" -> "tab1"))

// A parameterized reference to a table in a query. The table name is qualified and uses back-ticks.
spark.sql("SELECT * FROM IDENTIFIER(:mytab)", args = Map("mytab" -> "`default`.`tab1`"))

// You cannot qualify the IDENTIFIER clause or use it as a qualifier itself.
spark.sql("SELECT * FROM myschema.IDENTIFIER(:mytab)", args = Map("mytab" -> "`tab1`"))

spark.sql("SELECT * FROM IDENTIFIER(:myschema).mytab", args = Map("myschema" -> "`default`"))

// A parameterized column reference
spark.sql("SELECT IDENTIFIER(:col) FROM VALUES(1) AS T(c1)", args = Map("col" -> "t.c1"))

// Passing in an aggregate function name as a parameter
spark.sql("SELECT IDENTIFIER(:agg)(c1) FROM VALUES(1), (2) AS T(c1)", args = Map("agg" -> "max"))

SQL


-- Using a catalog using a variable.
> DECLARE mycat = 'main';
> USE CATALOG IDENTIFIER(mycat);

-- Creation of a table using variable.
> DECLARE mytab = 'tab1';
> CREATE TABLE IDENTIFIER(mytab)(c1 INT);

-- Altering a table with a fixed schema and a parameterized table name.
> ALTER TABLE IDENTIFIER('default.' || mytab) ADD COLUMN c2 INT;

-- Altering a table with a fixed schema and a parameterized table name in :re[DBR] 18.0 and above.
> ALTER TABLE IDENTIFIER('default.' || mytab) ADD COLUMN c2 INT;

-- Inserting using a parameterized table name. The table name is qualified and uses back-ticks.
> SET VAR mytab = '`default`.`tab1`';
> INSERT INTO IDENTIFIER(mytab) VALUES(1, 2);

-- A parameterized reference to a table in a query.
> SELECT * FROM IDENTIFIER(mytab);
  1   2

-- Dropping a table with separate schema and table parameters.
> DECLARE myschema = 'default';
> SET VAR mytab = 'tab1';
> DROP TABLE IDENTIFIER(myschema || '.' || mytab);
-- In :re[DBR] 18.0 and above:
> DROP TABLE IDENTIFIER(myschema '.' mytab);

-- You cannot qualify the IDENTIFIER clause or use it as a qualifier itself prior to :re[DBR] 18.0.
> SELECT * FROM myschema.IDENTIFIER('tab');
Error: PARSE_SYNTAX_ERROR

> SELECT * FROM IDENTIFIER('default').mytab;
Error: PARSE_SYNTAX_ERROR

-- A parameterized column reference
> DECLARE col = 't.c1';
> SELECT IDENTIFIER(col) FROM VALUES(1) AS T(c1);
  1

-- Passing in an aggregate function name as a parameter
> DECLARE agg = 'max';
> SELECT IDENTIFIER(agg)(c1) FROM VALUES(1), (2) AS T(c1);
  2

-- Specifying a SQL script label in :re[DBR] 18.0 and above.
> BEGIN
    IDENTIFIER('my_loop'): LOOP
      LEAVE IDENTIFIER('my_loop');
    END LOOP IDENTIFIER('my_loop');
    VALUES (42);
  END;
  42

Python

# Creation of a table using parameter marker.
spark.sql("CREATE TABLE IDENTIFIER(:mytab)(c1 INT)", args = {"mytab": "tab1"})

# Altering a table with a fixed schema and a parameterized table name.
spark.sql("ALTER TABLE IDENTIFIER('default.' || :mytab) ADD COLUMN c2 INT", args = {"mytab": "tab1"})

# Altering a table with a fixed schema and a parameterized table name in :re[DBR] 18.0 and above.
spark.sql("ALTER TABLE IDENTIFIER('default.' :mytab) ADD COLUMN c2 INT", args = {"mytab": "tab1"})

# Dropping a table with separate schema and table parameters.
spark.sql("DROP TABLE IDENTIFIER(:myschema || '.' || :mytab)", args = {"myschema": "default", "mytab": "tab1"})

# Dropping a table with separate schema and table parameters in :re[DBR] 18.0 and above.
spark.sql("DROP TABLE IDENTIFIER(:myschema '.' :mytab)", args = {"myschema": "default", "mytab": "tab1"})

# A parameterized reference to a table in a query. The table name is qualified and uses back-ticks.
spark.sql("SELECT * FROM IDENTIFIER(:mytab)", args = {"mytab": "`default`.`tab1`"})

# You cannot qualify the IDENTIFIER clause or use it as a qualifier itself.
spark.sql("SELECT * FROM myschema.IDENTIFIER(:mytab)", args = {"mytab": "`tab1`"})

spark.sql("SELECT * FROM IDENTIFIER(:myschema).mytab", args = {"myschema": "`default`"})

# A parameterized column reference
spark.sql("SELECT IDENTIFIER(:col) FROM VALUES(1) AS T(c1)", args = {"col": "t.c1"})

# Passing in an aggregate function name as a parameter
spark.sql("SELECT IDENTIFIER(:agg)(c1) FROM VALUES(1), (2) AS T(c1)", args = {"agg": "max"})