The SELECT statement in exists cannot be used to assign values.

Source: Internet
Author: User

Is the following sectionCode:

If   Exists ( Select   @ Myvalue   = Myvalue From Testtable Where Condition =   ' ABC ' )
Begin
Print   @ Myvalue  
End

When debugging in SSMs, an error is reported:

Message 102 , Level 15 , Status 1 , No 3 Line
' = ' There is a syntax error nearby.

This error should be reported by the equal sign, because the following debugging is performed:

If   Exists ( Select   1   From Testtable Where Condition =   ' ABC ' )
Begin
Print   ' Succeed! '
End

There is no problem, so I am depressed. I directly run the exists content separately, and there is no problem, and there is only one result. After searching for a long time on the Internet, there was no result. It was said that exists and select into had a conflict. I don't know the specific cause.

But fortunately there is still a solution, at least I am not too stupid, the following code can achieve the same effect:

Select   @ Myvalue   = Myvalue From Testtable Where Condition =   ' ABC '
If   @ Myvalue   Is   Not   Null
Begin
Print   @ Myvalue  
End

If any expert knows the reason, give me some guidance.

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.