Indicates the combination status by location in SQL Server.

Source: Internet
Author: User

In the past, system permissions or status were expressed by different fields or a permission string to indicate combined permissions. A few days ago, when developers of the tax department developed a system together, it is found that they use an int field to represent multiple states, and use Sybase to check by location. So I came back and used SQL Server2000 for a test. This method is also supported on SQL Server. (The younger brother has little knowledge before. Don't throw eggs if the prawns are used more often)

First, we design a Status field rule in bits.

Location 4 3 2 1
Status Disable Display Important Retained

Note:
Position: it is the serial number of the bits in a field from right to left.
Status: each bit indicates the permission. 1 indicates the permission. 0 indicates that the permission is not granted.
Reserved bits are used for other purposes and are not used in this example.

Sample Database Design

Field name Field Type Default Value
AppObject Varchar (64)
Status Int 15

The default value of Status permission is 15, indicating that all four of the Status fields are 1.

Data Table Sample Data

App1 15
App2 15
App3 7
App4 7

Use SQL statements to retrieve objects in the corresponding State
1. All enabled and enabled objects

Select * from AppObjects Where Status & 12 = 12

2. All enabled objects, including and not displayed objects

Select * from AppObjects Where Status & 8 = 8

3. All enabled, important, but not displayed objects

Select * from AppObjects Where Status & 10 = 10

Other combinations are based on principles and so on. Some may not be clear, but I just wrote it to let beginners know that there is such an implementation method.

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.