你可以在列執行關於列中的值的uniquenss創建唯一索引和您可以使用下面的代碼它調整到你的數據庫的規格, 戰國由於我已將事務隔離級別設置爲SERIALIZABLE,因此如果multipule用戶嘗試添加數據,它會減慢查詢速度,因爲當此事務隔離級別設置爲SERIALIZABLE時,它不允許其他用戶更改或向表中添加數據。 。
CREATE Procedure usp_InsertSID
@SID varchar(225)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
IF @SID IS NULL
RAISERROR('Validation Failed: SID is cannot be null', 16,1)
IF EXISTS (SELECT SID from tableName
WHERE SID= @SID)
RAISERROR('Validation Failed: SID already Exists',16,1)
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION
INSERT INTO tableName(ColumnNames)
VALUES (@SID)
COMMIT TRANSACTION
SELECT @ReturnMessage = 'Sucess! New SID is added.'
END TRY
BEGIN CATCH
IF (@@TRANCOUNT > 0)
ROLLBACK TRAN
SELECT @ReturnMessage = ERROR_MESSAGE()
SELECT @ReturnCode AS ReturnCode, @ReturnMessage AS ReturnMessage,
ERROR_LINE() AS ErrorLine,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState
END CATCH
END
GO
只要執行'ALTER TABLE dbo.YourTable ADD CONSTRAINT UQ_YourTable_YourColumnName UNIQUE(YourColumnName)',你就完成了SQL Server –