What is the difference between writing SQL statements directly in PL/SQL and using the EXECUTE IMMEDIATE method?

Source: Internet
Author: User
Tags sql using

The difference between writing SQL statements directly in PL/SQL and using the execute immediate method is that when executing SQL statements in PL/SQL, you can directly write SQL statements or splice an SQL statement into a string, as shown below: select * from dual; v_ SQL: = 'select * from dual'; execute immediate v_ SQL; generally, the performance of writing SQL directly is higher than that of spelling strings, if the oracle mechanism is automatically mobilized internally to execute String concatenation, the system parses the string and maps it to an SQL statement before executing it. However, the SQL spelling method is advantageous. That is to say, an SQL statement is a string that can be dynamically spliced and changed based on different conditions. This is not achieved by Directly Writing SQL statements. Another benefit of SQL spelling is v_ SQL: = 'select * from tables t where t. c_date =: 1 and t. name =: 2'; execute immediate v_ SQL USING '000000', 'xiaoming'; dynamically transmits values to parameters, which is the biggest advantage. Define a variable v_date varchar2 in PL/SQL. If you write SQL directly, you can directly write the variable v_date. If it is a spelling SQL statement, it is v_ SQL: = 'select * from tables t where t. c_date = ''' | v_date | '''; in this way, the SQL statement printed during execution is select * from tables t where t. c_date = '000000' if it is v_ SQL: = 'select * from tables t where t. c_date = '| v_date | ''; the SQL statement printed during execution is select * from tables t where t. c_date = 20130304; here, if c_date is of the varchar2 type, oracle needs to implicitly convert numbers to strings, affecting performance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.