Execute sp_executesql concatenate and execute SQL statements

Source: Internet
Author: User

Execute execute the concatenated SQL statement

Execute (SQL) indicates executing a string SQL

Execute SQL indicates the stored procedure, which is named SQL.

Run the string SQL statement.

Declare @ value nvarchar (100 );
Declare @ SQL nvarchar (199 );
Set @ SQL = n' select top 1 name from Admin where name = '+ @ value +''
Execute (@ SQL)

The parameter to be output cannot be executed here. Maybe I won't write it. If someone can write it, let me know. Thank you.

Below we can execute parameters with output values

Sp_executesql

Declare @ value nvarchar (100 );
Declare @ SQL nvarchar (199 );
Declare @ parmdefinition nvarchar (500 );
Set @ SQL = n' select top 1 @ value = Name from admin'
Set @ parmdefinition = n' @ value varchar (30) output'; -- Define parameters
Exec sp_executesql @ SQL, @ parmdefinition, @ value output -- @ value output provides output parameters
Select @ Value

Syntax

sp_executesql [ @stmt = ] stmt[     {, [@params=] N'@parameter_name data_type [ OUT | OUTPUT ][,...n]' }      {, [ @param1 = ] 'value1' [ ,...n ] }]

The SQL statement that stmt executes.

Unicode string that contains a Transact-SQL statement or batch processing.StmtIt must be a unicode constant or a unicode variable. More complex Unicode expressions are not allowed (for example, using the + operator to connect two strings ). Character constants are not allowed. If Unicode constants are specified, you must useNAs the prefix. For example, Unicode Constants
N'sp _ Who'Is valid, but the character constant'SP _ Who'Invalid. The size of the string is limited only by the memory of the available database server. In a 64-bit server, the string size is limited to 2 GB, that is
Nvarchar (max).

@ Params: @ parmdefinition of the above Column

SQL statement or batch processing does not contain parameters@ ParamsIt can be blank. If the SQL statement contains a parameter that must be defined, the parameter such as @ value in the column must be defined as @ value varchar (30) Output

@Params1 input parameters are as follows:@ Value output: the input value is not used here, so there is no value. Only the output value is required. To enter a value, you can write it@ Value = @ value output remove all outputs if no output is required

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.