冒号“:”在 SQL 查询中有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2177978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:15:09  来源:igfitidea点击:

What does the colon sign ":" do in a SQL query?

sqloracleplsqlbind-variables

提问by Jla

What does :stand for in a query?

什么是:代表在查询?

INSERT INTO MyTable (ID) VALUES (:myId)

How does it fetch the desired value?

它如何获取所需的值?

Edit:Also what is that sign called? I wanted to search on google, but what's the name for :?

编辑:还有那个标志叫什么?我想在谷歌上搜索,但它的名字是:什么?

采纳答案by OMG Ponies

That's called a bind variablein Oracle.

这在 Oracle 中称为绑定变量

what's the name for ":"?

“:”的名字是什么?

Colon.

冒号。

回答by Jeffrey Kemp

What does ":" stand for in a query?

“:”在查询中代表什么?

A bind variable. Bind variables allow a single SQL statement (whether a query or DML) to be re-used many times, which helps security (by disallowing SQL injection attacks) and performance (by reducing the amount of parsing required).

一个绑定变量。绑定变量允许多次重复使用单个 SQL 语句(无论是查询还是 DML),这有助于安全性(通过禁止 SQL 注入攻击)和性能(通过减少所需的解析量)。

How does it fetch the desired value?

它如何获取所需的值?

Before a query (or DML) is executed by Oracle, your program will create a cursor. Your program issues the SQL to be parsed for that cursor, then it must bind values for each bind variable referred to by the SQL. How this is done depends on the language.

在 Oracle 执行查询(或 DML)之前,您的程序将创建一个游标。您的程序发出要为该游标解析的 SQL,然后它必须为 SQL 引用的每个绑定变量绑定值。这是如何完成的取决于语言。

What is that sign called?

那个牌子叫什么?

A colon.

一个冒号。

回答by RAHUL

Colon :is used in HQL Hibernate Query Language to signify that there is a parameter involved.

:在 HQL Hibernate 查询语言中使用冒号来表示涉及一个参数。

So what that means is: SQL SYNTAX:

所以这意味着: SQL 语法:

SELECT * FROM EMPLOYEE WHERE EMP_ID = empID

is same as HQL SYNTAX:

与 HQL 语法相同:

SELECT * FROM EMPLOYEE WHERE EMP_ID = :empID

empIDbeing local variable for parameters...

empID作为参数的局部变量...

Hope this helps.

希望这可以帮助。

回答by Will Vousden

This is a tag for a named query parameter, and is not part of the query's actual syntax. The tag is replaced with some value specified in the code that makes the query before it is actually run.

这是命名查询参数的标记,不是查询实际语法的一部分。该标记被替换为在实际运行之前进行查询的代码中指定的某个值。

回答by kgiannakakis

It is a named parameter.

它是一个命名参数

In C#, you prefix the parameter with @ (see here).

在 C# 中,您使用 @ 作为参数前缀(请参阅此处)。

回答by Natty

Consider the following statements

考虑以下语句

select name from T_emp where id=1;
select name from T_emp where id=2;
select name from T_emp where id=3;

Each time a statement is executed, Oracle checks for previous occurrences of the same query. If it finds the same query, it makes use of the same execution plan. If not, It has to find the various execution paths, come up with the optimal execution plan and execute it.

每次执行一条语句时,Oracle 都会检查先前出现的相同查询。如果找到相同的查询,则使用相同的执行计划。如果没有,它必须找到各种执行路径,提出最佳执行计划并执行它。

Unlike human's it not intelligent enough to realize that only the id has changed(as per above example). Hence it go through all the struggles and executes it.

与人类不同的是,它不够聪明,无法意识到只有 id 发生了变化(如上例所示)。因此,它经历了所有的斗争并执行了它。

But there's a way to tell Oracle that its a similar statement and that it can use the same execution plan - BIND VARIABLE. Please find the example below:

但是有一种方法可以告诉 Oracle 它是一个类似的语句并且它可以使用相同的执行计划 - BIND VARIABLE。请在下面找到示例:

declare
  v_id number;
  v_name varchar2(30);
  type c is ref cursor;
  c1 c;
begin
  for i in 1..100
   loop
    open c1 for 'select * from T_emp where id = :x' using i;
    fetch c1 into v_name;
    dbms_output.put_line('name is ' || v_name);
   end loop;
END;

Using Bind variables helps to improve the performance tenfold. PL/SQL does use the bind variables on its own(you need not explicitly tell it)

使用绑定变量有助于将性能提高十倍。PL/SQL 确实使用了它自己的绑定变量(你不需要明确告诉它)

回答by Leslie

that's also the parameter syntax for a Delphi query

这也是 Delphi 查询的参数语法