How SQL Server handles when a SELECT statement returns multiple records to assign a value to a variable

Source: Internet
Author: User
Tags rowcount
select|server| variable | statement declare @user nvarchar (20)
Select @user = ' User1 '
Use CountMe
/* To determine whether the user exists * *
DECLARE @userweek int
Select @userweek =[week] from base where userid= @user
SELECT @ @rowcount
If @ @rowcount =0
Return
Select @userweek

The result of the final @userweek print is to record the value of the week field with the user name User1.

And when we select all the records with the following statement (no where limit)
declare @user nvarchar (20)
Select @user = ' User1 '
Use CountMe
/* To determine whether the user exists * *
DECLARE @userweek int
Select @userweek =[week] from base
SELECT @ @rowcount
If @ @rowcount =0
Return
Select @userweek

The last @userweek print value is the week field value of the last record in all returned records



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.