我有一个Client表,其中包含ClientID,UniqueId,Name,Address等字段.AdginidID,Name,Address和其他信息都是从UI填充的。我想使用触发器自动填充SQL Server中的ClientID字段(不播种,而不是null)。我测试了一些代码(见下文),但它们似乎没有用。有什么建议吗?
CREATE TRIGGER [dbo].[UpdateClientID]
ON [dbo].[Client]
AFTER INSERT
AS
BEGIN
DECLARE @Max INT;
DECLARE @Increment INT;
SET @Max = 0;
SET @Increment = 1;
SELECT *
INTO #temp
FROM inserted
SELECT @Max = MAX(ClientID)
FROM Client
WHILE (SELECT Count(*) FROM #temp WHERE ClientID IS NULL) > 0
BEGIN
UPDATE #temp
SET ClientID = @Max + @Increment
SET @Increment = @Increment + 1
END
UPDATE c
SET ClientID = t.ClientID
FROM Client c
INNER JOIN #temp t on c.UnqiueID = t.UniqueID
END