An interesting SQL proposition toggles the true and false value of bit type with a single statement _mssql

Source: Internet
Author: User
Tags switches
Of course, the first reaction is to use a stored procedure. Determine the original value of this field, and then update.
A rough search on the web did not find a solution. I have a very interesting result when I do it, and I don't even have a where.
The bit type assignment to the database is done using SQL statements and different input from the admin interface. Admin interface input must be filled in with true or false. And the SQL statement uses 0 or 1
So, first try:
Copy Code code as follows:

Updat T set b=b+1 where id=1;
Select B from t where id=1;

b field has been kept 1
So with the minus sign?
Copy Code code as follows:

Update T set b=b-1 where id=1;
Select B from t where id=1;

You can see that each result is switched between 0 and 1, and SQL Server treats-1 as a handle.
The demand was easily solved.
This is usually used where some state switches (or switches) are needed, and there is no need to know what happens after the switch in the requirements. Very interesting and very simple, hehe

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.