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.