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