Change uncertainty to confirmation ~ Can SQLSERVER perform bitwise operations?

Source: Internet
Author: User
Returning to the directory sometimes, when designing the table structure, some fields may have several statuses at the same time. For example, the file attributes of Xiaowei are also in this situation. They are read-only, archived, and hidden, but when you set attributes for a file, you can actually grant these three attributes to an object at the same time. This is normal for us. How can we implement these attributes in the program?

Returning to the directory sometimes, when designing the table structure, some fields may have several statuses at the same time. For example, the file attributes of Xiaowei are also in this situation. They are read-only, archived, and hidden, but when you set attributes for a file, you can actually grant these three attributes to an object at the same time. This is normal for us. How can we implement these attributes in the program?

Back to directory

Sometimes, when designing a table structure, some fields may have several statuses at the same time. For example, the file attributes of Xiaowei are also in this situation. "Read-only, archive, hide ", but when you set attributes for a file, you can actually grant these three attributes to an object at the same time. This is normal for us. How can we implement these attributes in the program?

First, an enumeration. The SQL statement may be a table or a specified number of values, such as read-only 1, archive 2, and hide 3.

I don't think the. net source code is stored in this way.

File Attribute: Read-Only + archive; Value: 1, 2

We can see that it is used, separated, and then separately summed.

But after I read the. net source code, my practice has changed, that is, using the displacement operation, we should say that there is a leap from performance to program definition.

The file attribute is read-only and archive. The value is 3 (3 = 1 + 2)

Of course, how can we achieve this when we need to query records containing the attributes of archive (2) in the database?

In fact, the offset operator numbers in SQL and C # are exactly the same. It has the following situations:

1 update User_Info set Status = Status | 8 where UserID = 1 -- update the status field in user_info and add 82 3 update User_Info set Status = Status &(~ 8) where UserID = 1 -- update the status field in user_info and subtract 8 from the original value. If the original value does not contain 8, add 8 4 5 select * from User where Status & 8> 0 to the original value -- find the records with 8 contained

Well, it's quite convenient. It's just superficial. In fact, its query performance is far higher than that of the first character concatenation method.

Back to directory

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.