您可以在列上创建唯一索引以强制列上值的唯一性,并且您可以使用以下代码将其调整为您的数据库规范,
Warring由于我已将事务隔离级别设置为 SERIALIZABLE,如果出现以下情况,它可能会减慢查询速度多个用户尝试添加数据,因为当此事务隔离级别设置为 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