如何使用存储过程以编程方式生成表的ID并插入?

时间:2018-11-21 05:48:32

标签: sql-server stored-procedures insert

我已经在数据库中创建了一个Student表,并且在插入新行时需要使用存储过程来生成唯一的StudentID,该行应从005开始直至025,包括025。 / p>

create procedure FillStudent
as
    insert into Student(StudentID)
    values (005)
go

但是我在想,如果它变成2位数怎么办?它会自动执行010吗?还是0010?如果这将是最后一个,使它像“ 010”这样的最佳方法是什么?我正在使用SQL Server。

2 个答案:

答案 0 :(得分:0)

只需将您的library(data.table) #data.table 1.11.4 Latest news: http://r-datatable.com FruitBought <- fread("name,Date,Qty Apple,20180101,15 Apple,20180105,20 Banana,20180102,18 Banana,20180109,14") #order is important before doing cumsum setorder(FruitBought, name, Date) fruitSold <- fread("Date,name,sold 20180101,Apple,5 20180102,Apple,3 20180102,Banana,3 20180103,Apple,1 20180103,Banana,4 20180104,Apple,2 20180104,Banana,2 20180105,Apple,1 20180105,Banana,2 20180106,Apple,2 20180106,Banana,3 20180107,Apple,2 20180107,Banana,1 20180108,Apple,0 20180108,Banana,3 20180109,Apple,2 20180109,Banana,1 20180110,Apple,3 20180110,Banana,1") #order is important before doing cumsum setorder(fruitSold, Date, name) 列设为身份列即可。在您的情况下,将种子值设置为5并增加为1。

StudentID

然后,SQL Server将自动为您填充该列。只需忽略您的[StudentID] [int] IDENTITY(5,1) NOT NULL语句中的该列即可。

请不要尝试将前导零添加到表中存储的实际数据中。那不是一个好方法。推荐的方法是通过在检索时格式化数据来达到这些要求。您可以通过以下方法实现它。

INSERT

答案 1 :(得分:0)

播放以下代码:

CREATE SEQUENCE [dbo].[StudentsID] 
START WITH 5
INCREMENT BY 1
MAXVALUE 25;

CREATE TABLE [dbo].[Student]
(
    [StudentID] CHAR(3)
);


GO

CREATE PROCEDURE [dbo].[FillStudent]
AS
BEGIN;
    INSERT INTO [dbo].[Student] ([StudentID])
    VALUES (RIGHT(NEXT VALUE FOR  [dbo].[StudentsID] + 1000, 3));
END;
GO

GO

EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];
EXEC [dbo].[FillStudent];

SELECT *
FROM [dbo].[Student];



DROP TABLE [dbo].[Student];
DROP PROCEDURE [dbo].[FillStudent];
DROP SEQUENCE [dbo].[StudentsID];

enter image description here

这个想法是使用sequence对象。

如果需要使用标识列,则可以创建具有标识列,计算列(这是您需要的值)和检查约束的表,以在达到id = 25后停止添加记录。执行以下脚本:

CREATE TABLE [dbo].[Student]
(
     [ID] TINYINT IDENTITY(5, 1)
    ,[StudentID] AS RIGHT([ID] + 1000, 3) PERSISTED
    ,CONSTRAINT CK_Student CHECK  ([ID] < 26)
);

INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;
INSERT INTO [dbo].[Student] DEFAULT VALUES;


SELECT *
FROM [dbo].[Student]

DROP TABLE [dbo].[Student]; 

在记录ID = 25之后,您会看到一个错误。