Declare @ Rsql Varchar ( 250 )
Declare @ Csql Varchar ( 300 )
Declare @ RC Nvarchar ( 500 )
Declare @ Cstucount Int
Declare @ Ccount Int
Set @ Rsql = ' (Select classroom_id from ea_roomtime where ZC = ' + @ ZC + ' And XQ = ' + @ XQ + ' And t ' + @ Time + ' = '' No '' ) And classroomtype = '' 1 '''
-- Exec (@ rsql)
Set @ Csql = ' Select @ A = sum (teststucount), @ B = sum (classcount) from ea_classroom where classroom_id in '
Set @ RC = @ Csql + @ Rsql
Exec Sp_executesql @ RC , N ' @ A int output, @ B INT output ' , @ Cstucount Output, @ Ccount Output -- Put exec results into Variables
-- Select @ csql + @ rsql
-- Select @ cstucount
In the databaseProgramDuring development, we often encounter the use of exec to execute an SQL statement that needs to return some values (usually used to construct dynamic SQL statements ), or, in a stored procedure, you can use exec to call another stored procedure with a returned value (the returned value must be obtained). How can you obtain the returned values?
1. Execution of SQL statements by Exec
The above @ rc SQL statement is used to find out the number of free classrooms in a specific period of time and the number of students that these classrooms can accommodate, because it involves the structure of dynamic SQL statements (the condition in @ csql contains a column name that changes dynamically), it should be executed in exec, but at the same time I have to return two results, soCodeIs:
http://www.cnblogs.com/xiaofengfeng/archive/2011/03/25/1995971.html