Obtain the exec value in SQL Server

Source: Internet
Author: User
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

Related Article

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.