gpt4 book ai didi

sql - 员工 ID 自动生成带前缀

转载 作者:行者123 更新时间:2023-12-04 19:21:44 31 4
gpt4 key购买 nike

我很困惑我希望员工 ID 是自动生成的前缀格式我知道在 sql server 中触发之前是可能的我正在关注帖子 http://www.aspdotnet-suresh.com/2012/04/set-custom-auto-generatedincrement.html

USE [test1]
GO

/****** Object: Table [dbo].[Users] Script Date: 03/08/2013 12:28:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Users](
[UserId] [varchar](50) NOT NULL,
[UserName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[Location] [varchar](50) NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

显示是

    UserId  UserName    LastName    Location
08U13000 SureshDasari Dasari Chennai
08U13001 SureshDasari Dasari Chennai
08U13002 SureshDasari Dasari Chennai
08U13003 SureshDasari Dasari Chennai
08U13004 SureshDasari Dasari Chennai
08U13005 SureshDasari Dasari Chennai
08U13006 SureshDasari Dasari Chennai
08U13007 SureshDasari Dasari Chennai
08U13008 SureshDasari Dasari Chennai
08U13009 SureshDasari Dasari Chennai
08U13010 SureshDasari Dasari Chennai
08U13011 SureshDasari Dasari Chennai
08U13012 SureshDasari Dasari Chennai
08U13013 SureshDasari Dasari Chennai
08U13014 SureshDasari Dasari Chennai
08U13015 SureshDasari Dasari Chennai

How to automatically create the primary key that has the following series A001000001 ... A001xxxxxx in SQL?

但我想在触发前插入这个触发器在同一个表和同一行中工作前 -

INSERT INTO Users (UserName,LastName,Location) VALUES('SureshDasari','Dasari','Chennai')

和用户 ID 自动创建?喜欢--08U13015,08U13014

我是从insert into inserted table做的但是错误不能insert in inserted or deleted table
或任何其他方法请帮我提前致谢

最佳答案

@marc_s提供的答案问题是如何做到这一点。

在您的情况下,它看起来像这样:

create table Users
(
Id int identity (3000, 1),
UserId as '08U1'+right('0000'+cast(Id as varchar(5)), 5) persisted,
UserName varchar(50),
LastName varchar(50),
Location varchar(50),
constraint PK_Users primary key (UserId)
)

我不会像建议的那样做 here .如果您曾经删除一行或在并发的情况下,您将有重复项。

SQL Fiddle

关于sql - 员工 ID 自动生成带前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288625/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com