what is the difference between the & and : in PL/SQL in oracle?

*爱你&永不变心* 提交于 2020-01-16 05:30:07

问题


I am stuying PL/SQL & I am going through the bind variable . I understood that the bind variable we used to pass RUN-TIME values.

but what is the difference between & and :(colon) in PL/SQL? both are same or is their any difference between this two? when should I use & and : ?


回答1:


The & is used in SQL*Plus only, it has no meaning outside of it.

While SQL*Plus "parses" the input buffer, it replaces &variables with what they were defined to. See also this link and this link. The technical term is substitution variable.

On the other hand :variable are real bind variables. They are used when Oracle's SQL engine "parses" and executes the SQL statement. See for example this link or this link.

So, in short, &variables are replaced by SQL*Plus and then passed to Oracle's SQL engine, while :variables are left untouched by SQLPlus and passed to Oracle's SQL engine as they appear. Outside fo SQLPlus, & makes no sense.




回答2:


& has no meaning in PL/SQL, it is actually a SQL Plus feature (that has been copied in Toad, SQL Developer etc. for compatibility). In SQL Plus, & is used to define a substitution variable, which is replaced with specified replacement text by SQL Plus before the code is sent to the server to be processed.

The correct syntax for a SQL Plus substitution variable is &name. i.e. is begins with a & and ends with a ., but in most situations the . is optional. So when you run this code in SQL Plus:

select * from emp where ename = '&name.';

SQL Plus prompts the user for a value for name and substitues this into the query before it is run. So if the user enters KING then the SQL actually run is:

select * from emp where ename = 'KING';


来源:https://stackoverflow.com/questions/10172550/what-is-the-difference-between-the-and-in-pl-sql-in-oracle

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