生成随机的唯一字符?

时间:2018-10-17 17:12:57

标签: sql-server

出于我的应用目的,我需要通过对每个记录使用单个 CHAR字符来标识某些记录。我希望每当添加新记录时都能自动生成一个唯一字符,有点像普通的int类型自动增量ID。有没有办法做到这一点?选择什么角色对我来说并不重要,可以是随机的。

注意:限制对我而言并不重要,表很小,肯定不会超过200条记录。

2 个答案:

答案 0 :(得分:3)

也许只使用一个常规的IDENTITY字段,然后添加一个计算列即可根据IDENTITY计算您的角色?

这将给您几百行。满足您的要求。

declare @testTable as TABLE
(
    id int identity(65,1)
    , myData varchar(100)
    , idChar as char(id)
)

insert into @testTable (myData)
Values ('January'), ('February'), ('March'), ('April'), ('May'), ('June'), ('July'), ('August'), ('September'), ('October'), ('November'), ('December')
,('January'), ('February'), ('March'), ('April'), ('May'), ('June'), ('July'), ('August'), ('September'), ('October'), ('November'), ('December')
,('January'), ('February'), ('March'), ('April'), ('May'), ('June'), ('July'), ('August'), ('September'), ('October'), ('November'), ('December')
,('January'), ('February'), ('March'), ('April'), ('May'), ('June'), ('July'), ('August'), ('September'), ('October'), ('November'), ('December')
,('January'), ('February'), ('March'), ('April'), ('May'), ('June'), ('July'), ('August'), ('September'), ('October'), ('November'), ('December')
,('January'), ('February'), ('March'), ('April'), ('May'), ('June'), ('July'), ('August'), ('September'), ('October'), ('November'), ('December')


select * from @testable

结果:

id          myData                      idChar
----------- --------------------------- ------
65          January                     A
66          February                    B
67          March                       C
68          April                       D
69          May                         E
70          June                        F
71          July                        G
72          August                      H
73          September                   I
74          October                     J
75          November                    K

答案 1 :(得分:2)

创建一个以33开始的标识列(0-32是控制字符)。

使用公式Vue.component('ti-button', { props: ['button'], template: '#ti-button', mounted: function () { // ripple on button mdc.ripple.MDCRipple.attachTo(this.$el); }, methods: { buttonClicked: function() { this.$emit('button-clicked'); } } }); <script type="text/x-template" id="ti-page-inquire"> <div> <h3 class="mdc-typography--headline3">{{page.name}}</h3> <ti-button v-bind:button="page.button" v-on:button-clicked="onSubmit"></ti-button> </div> </script> <script type="text/x-template" id="ti-button"> <button class="mdc-button mdc-button--raised" v-bind:title="button.name" @clicked="buttonClicked">{{button.name}}</button> </script> 创建一个计算列。

在其他答案出现之前,我正在研究这个问题。