Execute SQL - SP with embedded parameters

xiaoxiao2021-03-06  71

Usually the SQL statement is implemented, and everyone is except that EXEC, which is powerful, but does not support embedding parameters, sp_executesql solves this problem. Copy SQLServer Help:

SP_EXECUTESQL

Perform a Transact-SQL statement or batch processing that can be reused or dynamically generated multiple times. Transact-SQL statements or batch can include embedded parameters.

grammar

sp_executesql [@stmt =] stmt [{, [@Params =] n '@ parameter_name data_type [, ... n]'} {, [@ param1 =] value1 '[, ... n]}]

parameter

[@Stmt =] STMT

The Unicode string containing Transact-SQL statements or batch, the STMT must be implicitly converted to NTEXT Unicode constants or variables. More complex Unicode expressions are not allowed (eg, two strings are used to use operators). The character constant is not allowed. If constant is specified, N must be used as a prefix. For example, Unicode constant n'sp_who 'is effective, but the character constant' sp_who 'is invalid. The size of the string is limited only by the available database server memory limit.

The STMT can contain the same parameters as the variable name, for example:

N'Select * from Employees where Employeeid = @IDParameter '

Each parameter contained in the STMT must have a corresponding item in the @Params parameter definition list and the parameter value list.

[@PARAMS =] n '@ parameter_name data_type [, ... n]'

String, which contains definitions of all parameters embedded in the STMT. This string must be an unicode constant or variable that can be implicitly converted to NTEXT. Each parameter definition consists of parameter names and data types. n is a placeholder that indicates additional parameter definitions. Each parameter specified in the STMT must be defined in @params. If the Transact-SQL statement or batch in the STMT does not contain parameters, it is not required to @params. The default value of this parameter is NULL.

[@ param1 =] 'Value1'

The value of the first parameter defined in the parameter string. This value can be a constant or variable. The parameter value must be provided for each parameter included in the STMT. If the TRANSACT-SQL statement or batch included in the STMT does not require a value.

n

Attach the value of the value of the parameter. These values ​​can only be constant or variables, and cannot be more complex expressions, such as functions or expressions generated using operators.

Returns the code value

0 (success) or 1 (failed)

Result set

Returns the result set from all SQL statements that generate SQL strings. Example (thank Zou Jian)

Declare @USER VARCHAR (1000) Declare @Motable VARCHAR (20) SELECT @Motable = 'MT_10'Declare @SQL NVARCHAR (4000) - Defines Variables, Note Type Set @ SQL =' SELECT @user = Count (DistINCT Userid) from ' @ Motable - Assigning Variables - Execute the statement exec sp_executesql @SQL in @SQL, n' @ user varchar (1000) Out '- Indicates that the statement in @SQL contains an output parameter, @ user out - - and call the stored procedure, specify the output parameter value Print @User this example, @ motet is embedded parameters.

转载请注明原文地址:https://www.9cbs.com/read-93673.html

New Post(0)