如何使用主键生成ID? [英] How to generate ID with primary key ?
本文介绍了如何使用主键生成ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Computed Column Specifier为主键添加前缀。
我知道有一个公式看起来像SL +'_'+ cast(id as varchar(20))但是这个公式无法由SQL Server Management Studio验证。
请帮我构建此公式。
我不喜欢现在使用编码来生成id。
我尝试了什么:
尝试将公式SL +'_'+ cast(id as varchar(20))输入到指定的计算列但由于格式错误而无法验证..
解决方案
您可以使用序列,请参阅此处的文章:
SQL SERVER 2012 - 如何生成varchar序列号 - 使用序列对象| SQL Server门户 [ ^ ]
并且: T-SQL中灵活的编号序列 [ ^ ]
但是只显示 SQL Server 2012 的序列...
Well I would like to add prefix to primary key using Computed Column Specifier.
I know there is a formula looks like SL+'_'+cast(id as varchar(20)) but this formula cannot be validated by SQL Server Management Studio.
Please help me to build this formula.
I dont prefer to use coding to generate id for now.
What I have tried:
Tried putting formula SL+'_'+cast(id as varchar(20)) in to Computed Column Specified but not validating due to wrong format..
解决方案
You could use sequences for this, see articles here:
SQL SERVER 2012 – How to generate a varchar Sequence Number – Using Sequence Object | SQL Server Portal[^]
And: Flexible numbering sequences in T-SQL[^]
But sequences are only supported from SQL Server 2012 apparently ...
这篇关于如何使用主键生成ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文