A small method to assign values to multiple parameters from the result set in the Stored Procedure

Source: Internet
Author: User
When writing a stored procedure, you need to query a result from the database and assign values to several parameters based on the result. In the past, this situation was rarely encountered. Occasionally, a cursor is used to operate multiple result sets. The performance of a query that returns only one result is definitely affected by the cursor. After testing, I finally found a simple method. I don't know if it is helpful to you. If you have a better method, please be sure to advise.

Example:

table structure
test
ID cloum1 cloum2
........................ ..................
1 A B
2 AA BB
3 cc
______________________________

Create proc Test

As

Declare @

Declare @ B

-- Now I want to query the values of the ID and cloum1 fields from the test table and assign them to the variables @ A and @ B.

Select @ A = ID, @ B = cloum1 from test where cloum2 = 'B'

You can assign the value of the result set to multiple variables with the preceding operations. In one sentence, it is much more convenient than using a cursor, and the performance is much better. If multiple result sets are returned, @, the value of @ B is the value of the last record in the result set.

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.