The use of sp_executesql

Source: Internet
Author: User

Requirements: The table name is dynamic, it must be executed with exec, and then the variable is dynamically assigned to the exec.

EXEC (' Select @count =count (empid) from ' [email protected]+ ' where proid= ' [email protected]+ ' and id< ' [email protected] + ' and state!=4 ')

The code below will be able to get rid of the stuff that's behind it.

One: Right

Set @sql =n ' select @count =count (empid) from ' [email protected]
EXEC sp_executesql @sql, N ' @count int output ', @count
Select @count

Two: Error
Set @sql =n ' select @count =count (empid) from ' [email protected]+ ' where proid= ' [email protected]+ ' and id< ' [email Protec ted]+ ' and state!=4 '
EXEC sp_executesql @sql, N ' @count int output ', @count
Select @count

Three: Correct (my example contains the table name, the table name can not be directly and proid1 the table name must be used ' + table name + ', no plus I tried the error)
declare @count int, @tableName nvarchar, @SQLString nvarchar (max), @proid int, @id int, @ParmDefinition nvarchar (max);
Set @tableName = ' Table27 ';
Set @proid = 433;
Set @id = 159;
--set @sql =n ' select @count =count (empid) from Table27 '
Set @SQLString =n ' select @countOUT =count (empid) from ' [Email protected]+ ' Where [email protected] and id< @id1 and state! =4 ';
Set @ParmDefinition =n ' @proid1 int, @id1 int, @countOUT int output ';
EXEC sp_executesql @SQLString, @ParmDefinition, @[email protected],@[email protected],@[email protected] output;
Select @count;

An official example:

DECLARE @IntVariable int;
DECLARE @SQLString nvarchar (500);
DECLARE @ParmDefinition nvarchar (500);
DECLARE @max_title varchar (30);

SET @IntVariable = 197;

From AdventureWorks.HumanResources.Employee
WHERE ManagerID = @level ';
SET @ParmDefinition = N ' @level tinyint, @max_titleOUT varchar (+) OUTPUT ';

EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @IntVariable, @[email protected]_title OUTPUT;
SELECT @max_title;
Explanation of MSDN:

Executes Transact-SQL statements or batches that can be reused or generated dynamically. A Transact-SQL statement or batch can contain embedded parameters.

MSDN Syntax:
sp_executesql [@stmt =] stmt


{, [@param1 =] ' value1 ' [,... N]}
]


@sqlstring: Is the SQL statement string you want to execute
@ParmDefinition: The parameters used in the @sqlstring declare the output parameters to be added

The first parameter, SqlString, is the executed SQL string.
The second parameter, @parmdefinition, is the parameter used in @sqlstring to declare the output parameter to be added
The parameters of the last parameter plus output are the parameters of the outputs (which need to be associated with external relative variables)
The parameters in the middle are the parameters used in the @sqlstring (need to be associated with external relative variables)
Finally you can query the select output parameters (select @count)

The use of sp_executesql

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.