Is there a way to generate an identity or sequence that increases strength 2?

In my code, I tend to use a lot of Flags enums that match the database table that contain all of their behavioral properties. Thus, behavioral properties can be used in both SP and C # code, which uses an enumeration.

My problem is with Enums that have the Flags attribute attached to them.

Is there a way to automatically generate permissions 2 as an identity column (or as a sequence) for such tables?

PS: currently I manually put the code in a small table - or programmatically in tables that change over time - I prefer to do this automatically

+4
source share
3 answers

As far as I know, the answer is NO. The identifier is intended to increase by the value of the seed you specify, which is equal to 1 by default. But you can have an identity in several of two. 2,4,6,8,10

id INT identity(2, 2) -- starts at 2, increments by 2 

If you need something else, you may need to implement your own.

Computed Columns

 create table t ( id int identity, power_id as power(2, id), name varchar(50) ) Results ID POWER_ID TEXT ------------------------------- 1 2 SHORT ----2^1 2 4 MEDIUM ----2^2 3 8 LONG ----2^3 
+5
source

In the "Identifier specification" section on the column properties (visible after clicking "Design" in your table), you can set "Identity Identification" to 2 to generate identifiers 1, 3, 5, 7, if this is what you are after?

0
source

No: read here

but you can save the identity as a surrogate key, then you use another column that is always equal to 2 ^ id (use a trigger to insert?) for your values โ€‹โ€‹2 ^ n or you could just use a simple identifier, because if you use the Flags attribute, you only need an exponent, right?

0
source

Source: https://habr.com/ru/post/1447198/


All Articles