How can I create table that automatically increments a second column (BitField) on the ID autoincrement?
ID: 0 -> BitField: 2^0 = 1
ID: 1 -> BitField: 2^1 = 2
ID: 2 -> BitField: 2^2 = 4
ID: 3 -> BitField: 2^3 = 8
ID: 4 -> BitField: 2^4 = 16
ID: 5 -> BitField: 2^5 = 32
ID: 6 -> BitField: 2^6 = 64
//etc
This for a user table field that is a bitmask (think along the lines of "permissions"). So a side question would be: Do you think having a one-to-many for the options and many-to-many table for granting to each user is a better implementation (not even sure how to then format all that info into one row so usin a bitmask)?
CREATE TABLE [dbo].[Test](
[Id] [int] NOT NULL,
[BitField] AS (power((2),[id]))
) ON [PRIMARY]
GO
-- Testing
INSERT INTO Test(Id) VALUES(2)
INSERT INTO Test(Id) VALUES(6)
INSERT INTO Test(Id) VALUES(8)
SELECT * FROM Test
Id BitField
2 4
6 64
8 256
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.