2
我創建了一個將作爲主鍵爲我的表違反約束並行運行
CREATE FUNCTION dbo.NewCustomerPK()
RETURNS VARCHAR (10)
AS
BEGIN
DECLARE @LastCustID VARCHAR(10)
DECLARE @newID INT
DECLARE @charID CHAR(10)
SELECT
@LastCustID = MAX(CustID)
FROM
dbo.TestCust
IF (@LastCustID IS NULL)
BEGIN
SET @LastCustID = 'CUST000001'
END
ELSE
BEGIN
SET @newID = RIGHT(@LastCustID, 6) + 1
SET @charID = 'CUST' + RIGHT(('0000000' + CONVERT(VARCHAR(6), @newID)), 6)
SET @LastCustID = @charID
END
RETURN @LastCustID
END
CREATE TABLE dbo.TestCust
(
CustID VARCHAR(10) PRIMARY KEY NOT NULL DEFAULT dbo.NewCustomerPK(),
Name VARCHAR(50)
)
功能並試圖插入測試數據
DECLARE @Counter INT = 1,
@Stopper INT = 500000
WHILE(@Counter <= @Stopper)
BEGIN
INSERT INTO dbo.TestCust(NAME)
VALUES('test'+CONVERT(VARCHAR(6), @Counter))
SET @Counter = @Counter + 1
END
它工作正常,但是當我嘗試並行運行(在新窗口中運行循環數據插入)會導致主約束違例錯誤
使其成爲觸發。 – Jester 2015-02-10 07:41:11