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

SqlOraclePlsqlBind Variables

Sql Problem Overview


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 :?

Sql Solutions


Solution 1 - Sql

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.

Solution 2 - Sql

That's called a bind variable in Oracle.

>what's the name for ":"?

Colon.

Solution 3 - Sql

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.

Solution 4 - Sql

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.

Solution 5 - Sql

It is a named parameter.

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

Solution 6 - Sql

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)

Solution 7 - Sql

that's also the parameter syntax for a Delphi query

Solution 8 - Sql

Found the first couple minutes of this video to be very useful: https://www.youtube.com/watch?v=K6VfcRALxW4

To extract: it's called a bind variable, this is a placeholder for the user input it's waiting to receive

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJlaView Question on Stackoverflow
Solution 1 - SqlJeffrey KempView Answer on Stackoverflow
Solution 2 - SqlOMG PoniesView Answer on Stackoverflow
Solution 3 - SqlRAHULView Answer on Stackoverflow
Solution 4 - SqlWill VousdenView Answer on Stackoverflow
Solution 5 - SqlkgiannakakisView Answer on Stackoverflow
Solution 6 - SqlNatty View Answer on Stackoverflow
Solution 7 - SqlLeslieView Answer on Stackoverflow
Solution 8 - SqlOnionView Answer on Stackoverflow