3

我很困惑,我希望员工 id 是自动生成的前缀格式我知道这可能在 sql server 中触发之前我关注帖子 http://www.aspdotnet-suresh.com/2012/04/set-custom-自动生成的increment.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

如何在SQL中自动创建具有以下系列A001000001 ... A001xxxxxx的主键?

但我想在触发器之前插入,并且这个触发器在同一个表和同一行中工作,例如 -

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

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

我是从插入到插入的表中做的,但是错误不能插入到插入或删除的表中
或任何其他方法请帮助我提前谢谢

4

1 回答 1

1

@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)
)

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

SQL小提琴

于 2013-03-08T07:46:34.373 回答