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

我的未来我决定 提交于 2019-11-27 12:35:09

That's called a bind variable in Oracle.

what's the name for ":"?

Colon.

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).

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.

What is that sign called?

A colon.

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.

RAHUL

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

So what that means is: SQL SYNTAX:

SELECT * FROM EMPLOYEE WHERE EMP_ID = empID

is same as HQL SYNTAX:

SELECT * FROM EMPLOYEE WHERE EMP_ID = :empID

empID being local variable for parameters...

Hope this helps.

It is a named parameter.

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

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.

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.

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:

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)

that's also the parameter syntax for a Delphi query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!