写了个行锁更新,求指正
环境 mssql2005
CREATE PROCEDURE CarToCompany_tbl_LockAdd
@CC_ID int,
@C_ID int
AS
Begin
DECLARE @isok int
Begin Try
Begin Transaction
SELECT
CC_ID,C_ID
FROM [CarToCompany_tbl] ROWLOCK
where CC_ID=@CC_ID and C_ID=@C_ID
if @@rowcount=0
begin
INSERT INTO [CarToCompany_tbl](
[CC_ID],[C_ID]
)VALUES(
@CC_ID,@C_ID
)
end
Commit Transaction
set @isok =1
End Try
Begin Catch
Rollback Transaction
set @isok = -1
print error_number()
print error_message()
print error_state()
print error_severity()
End Catch
return @isok
End
GO
------解决方案--------------------不加ROWLOCK应该也可以.
------解决方案--------------------OK ,我看行。
------解决方案--------------------需不需要加上这个With(RowLock,UpdLock)?
------解决方案--------------------毛啊,哪呢?而且还会产生唯一索引冲突。。
------解决方案--------------------菜鸟提问,MS-SQL不是自动控制锁机制吗,
不需要人为写个锁把
------解决方案--------------------CREATE PROCEDURE CarToCompany_tbl_LockAdd
@CC_ID int,
@C_ID int
AS
Begin
DECLARE @isok int
Begin Try
Begin Transaction
SELECT
CC_ID,C_ID
FROM [CarToCompany_tbl] with(ROWLOCK,Xlock)
where CC_ID=@CC_ID and C_ID=@C_ID
if @@rowcount=0
begin
INSERT INTO [CarToCompany_tbl](
[CC_ID],[C_ID]
)VALUES(
@CC_ID,@C_ID
)
end
Commit Transaction
set @isok =1
End Try
Begin Catch
Rollback Transaction
set @isok = -1
print error_number()
print error_message()
print error_state()
print error_severity()
End Catch
return @isok
End
这样才有意义,