In SQL Server, the execution result of Exec is assigned to the variable output in the stored procedure.

Source: Internet
Author: User
Background: In this case, a table name and table attributes are dynamically imported into the stored procedure (the primary key ID is used here ), use these two variables to find some fields of a piece of data, then use these to perform logical operations (your own logic), and finally output the results. If you don't want to talk about it, check whether it is the result you want:

Background: In this case, a table name and table attributes are dynamically imported into the stored procedure (the primary key ID is used here ), use these two variables to find some fields of a piece of data, then use these to perform logical operations (your own logic), and finally output the results. If you don't want to talk about it, check whether it is the result you want:

Background:

In such a situation: DynamicStorageProcessTo pass in the table name and some attributes of the table (the primary key ID is used here), and then use the twoVariableFind some field values of a piece of data, then use these values for logical operations (your own logic), and finallyResultOutput. Let's just talk about it. Let's see if it's what you want.Result:




Note: [region 1 is the table to be tested] [Region 2 is the table data] [region 3 is the jan + feb + mar column value and 5 of the table data]


StorageProcessThe Code is as follows:



It can be seen that since the values of jan, feb, and mar columns can be obtained, it is easy to make your own judgment later. I will not go into details.


Because the study is not refined, the above conclusion is inspired by others, the relevant link: http://www.cnblogs.com/wanyuan8/archive/2011/11/09/2243483.html

Below is a post posted by the original author (as a programmer, you know ):

Sp_executesql introduction and usage

Execute I believe everyone is familiar with it, abbreviated as exec, exceptRunStorageProcess, Generally usedRunDynamic SQL
Sp_executesql, a new system introduced in sql2005StorageProcessIt is also used to process dynamic SQL statements, such:
Exec sp_executesql @ SQL, n' @ count int out, @ id varchar (20) ', @ cou out
, @ Id
@ SQL: a dynamic SQL statement.
N' @ count int out, @ id varchar (20) 'indicates the list of parameters in the assembled dynamic SQL statement.
@ Cou out, @ id is the external parameter list that provides values for the parameter list in dynamic SQL.

So what are the differences between them?

1. The biggest difference between them is embedded parameters, as shown in the following statement.
Declare @ SQL nvarchar (2000)
Declare @ id varchar (20)
Set @ id = '1'
Set @ SQL = 'select count (*) from emp where id = '+ @ id
Exec @ SQL
I want to pass the obtained count (*). It is difficult to use traditional exec, but it is easy to use sp_executesql:
Declare @ SQL nvarchar (2000)
Declare @ cou int
Declare @ id varchar (20)
Set @ id = '1'
Set @ SQL = 'select @ count = count (*) from emp where id = @ id'
Exec sp_executesql @ SQL, n' @ count int out, @ id varchar (20) ', @ cou out
, @ Id
Print @ cou
2. Performance
As you can see, if exec is used, because the @ id passed in each time is different, the @ SQL generated each time is different.RunOne Sql2005 request must be re-submittedRunDynamic SQL recompile once
However, sp_executesql is different. Because the numerical parameters are parameterizedRunThe dynamic SQL will never change, but the value of the input parameter changes.RunIn seconds, it will be re-compiled, and the speed will naturally be much faster!

Note:
1. sp_executesql requires that the dynamic SQL and dynamic SQL parameter lists must be Nvarchar. For example, in the previous example, @ SQL, n' @ count int out, @ id varchar (20) 'I remember that Varchar can be used in sql2005, but it won't work after I patch Sp3. It must be Nvarchar.
2. The parameter list of dynamic SQL must be consistent with that of the parameter list of external values, for example:
N' @ count int out, @ id varchar (20) ', @ cou out, @ id
@ Count corresponds to @ cou, @ id corresponds to @ id
If they are inconsistent, they must be explicitly marked, for example:
N' @ count int out, @ id varchar (20) ', @ id = @ id, @ count = @ cou out
3. The parameter list of dynamic SQL can have the same name as the parameter list of external parameters.

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.