Bug in SQL Server

Source: Internet
Author: User

Today, customer feedback found that there is a bug in MS SQL, the problem is quite large, so issued to make a mark:
Open the SQL query window and paste in the following code:
DECLARE @i intset @i = 1while @i<5--loop execution 4 times begin DECLARE @aa varchar (1)  --Define variable print convert (varchar (1), @i) + '--' +is Null (@aa, ")  --Print variable Select @aa = ' 0 ' where 1>2  --to determine if 1 is greater than 2 is assigned to the variable, the argument is not set so the variable is still not assigned if @aa is null  -- If the variable does not fetch a value, the variable is assigned a value of 1 set @aa = ' 1 ' Set @i = @i + 1  --Loop End


run complete, find out what situation:
The
2--1
3--1
4--1 The variables defined inside the loop, the first time the variable is printed is really normal, because there is no assignment so it is empty,But after a few loops, it turns out to be 1.What's the situation? The system actually took the value of the last loop! SQL Server defines the variable in the loop, and if the variable is assigned at the end of the loop, it will cause the next loop to assign the value to the defined variable .Finally, there is no way to assign a value to a variable before performing a related operation:
DECLARE @i intset @i = 1while @i<5begin declare @aa varchar (1) Set @aa = ' Print convert (varchar (1), @i) + '--' +isnull (@a A, ') Select @aa = ' 0 ' where 1>2 if @aa is null set @aa = ' 1 ' Set @i = @i + 1end

Then run the following results:

The
2--
3--
4--

Alas, this question, Microsoft has a little water


Bug in SQL Server

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.