Dynamic SQL Execution parsing and passing parameters in SQL Server

Source: Internet
Author: User
Tags sql server query
Dynamic SQL: code that is executed dynamically. It is generally an SQL statement block dynamically combined based on user input or external conditions. Dynamic SQL can give full play to the powerful functions of SQL and easily solve problems that are difficult to solve by other methods. I believe that anyone who has used dynamic SQL can realize the convenience it brings. However, dynamic SQL is sometimes not as good as static SQL in execution performance (efficiency) and is not suitable for use, there are often security risks (SQL injection attacks ). Dynamic SQL can be executed through execute or sp_executesql. (From msdn)

Execute

Execute a command string or string in a Transact-SQL batch or execute one of the following modules: system stored procedures, user-defined stored procedures, scalar value user-defined functions, or extended stored procedures. SQL Server 2005 extends the execute statement to send commands to the linked server. In addition, you can explicitly set the context for executing strings or commands.

Sp_executesql

Execute a Transact-SQL statement or batch process that can be repeatedly used or dynamically generated multiple times. Transact-SQL statements or batch processing can contain embedded parameters. In terms of batch processing, name scope, and database context, sp_executesql performs the same behavior as execute. The Transact-SQL statement or batch processing in the sp_executesql stmt parameter is compiled only when the sp_executesql statement is executed. Then, the content in stmt will be compiled and run as an execution plan. This execution plan is independent of the Execution Plan for batch processing called sp_executesql. Sp_executesql
Batch Processing cannot reference variables declared in batches that call sp_executesql. The local cursor or variable in sp_executesql batch processing is invisible to the batch processing that calls sp_executesql. Changes made to the database context are valid only before the end of the sp_executesql statement.

If only the parameter values in the statement are modified, sp_executesql can be used to execute the transact-SQL statement multiple times instead of the stored procedure. Because the transact-SQL statement remains unchanged and only the parameter value changes, the SQL Server Query Optimizer may reuse the execution plan generated during the first execution.

In general, we recommend that you use sp_executesql to execute dynamic SQL statements. On the one hand, it is more flexible and can have input and output parameters. On the other hand, the query optimizer is more likely to reuse the execution plan, improve Execution efficiency. In addition, the use of sp_executesql can improve security. Of course, it does not mean that execute should be abandoned completely. In specific cases, execute is more convenient than sp_executesql. For example, dynamic SQL strings are of the varchar type and are not of the nvarchar type. Sp_executesql can only execute Unicode strings or constants or variables that can be implicitly converted to ntext, while execute can execute both types of strings.

Next we will compare some details of execute and sp_executesql.

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.