如何在sql server中生成字母数字代码? [英] How to generate an alphanumeric code in sql server?

查看:109
本文介绍了如何在sql server中生成字母数字代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个表 ClientInfo ,在此我想为 ClientCode 列生成一个字母数字代码。我想生成的代码应该像 SAPL-LPS-00001

SAPL是一个静态的四字母字符串。

LPS是前三个组织信。

00001在标识栏的基础上递增。

为此我在创建表向导中写了一些代码但是我我收到了一个错误。我正在向你发送我的创建表代码。

还有一件事来自前端 OrganizationName



I am Creating a table ClientInfo, in this i want to generate a alphanumeric code for ClientCode column. The Code whic i want to generate should be like SAPL-LPS-00001
SAPL is a static four letter string.
LPS is first three letter of organization.
00001 is incremented on the basis of identity column.
for this i have wrote some code in create table wizard but i am getting an error. I am sending my create table code to you.
One more thing OrganizationName coming from front end.

Create table ClientInfo
(
ClientId int identity(1,1) primary key,
ClientCode As ('SAPL-' +Right(substring(OrganizationName, 1,3) +Right('00000' +Convert(varchar, ClientId,0),(5)),3),
OrganizationName varchar(100))

推荐答案

在插入后的触发器中的更新查询中使用它

Use this in update query in trigger after insert
'SAPL-' + substring(OrganizationName, 1, 3) + '-' + Right('00000' + Convert(varchar, ClientId, 0), (5))


见:使用SQL Server自定义自动生成的序列 [ ^ ]



问候..
See this : Custom Auto-Generated Sequences with SQL Server[^]

Regards..


这篇关于如何在sql server中生成字母数字代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆