An interesting SQL proposition uses a statement to switch the true/false values of the BIT type.

Source: Internet
Author: User

Of course, the first reaction is to use stored procedures. Determine the original field value and then UPDATE it.
I cannot find a solution after a rough query on the Internet. I have a very interesting result, and I don't even need to use WHERE.
Assign values to the BIT type of the database. use SQL statements to operate the database and use the management interface to input different values. The management interface must be set to TRUE or FALSE. SQL statements use 0 or 1
Therefore, first try:
Copy codeThe Code is as follows:
Updat t set B = B + 1 where id = 1;
Select B from t where id = 1;

Field B remains 1
So what about using minus signs?
Copy codeThe Code is as follows:
Update t set B = B-1 where id = 1;
Select B from t where id = 1;

We can find that each result is switched between 0 and 1, and SQL SERVER treats-1 as 1.
This requirement can be easily solved.
This is usually used in some status switches (or switches). You do not need to know the status after the switch. Very interesting and simple.

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.