On 31 Mar 2005 14:17:10 -0800, ajamrozek wrote:
Ok so here's my stored procedure:
--------------------------------------
(
@NSIR_ID INT,
@PRODUCT_DESCRIPTION NVARCHAR(255),
@ACCESS_TIMESTAMP TIMESTAMP
)
AS
BEGIN TRAN
UPDATE ajamroz.IT_NSIR
SET PRODUCT_DESCRIPTION = @PRODUCT_DESCRIPTION
WHERE ID = @NSIR_ID AND ACCESS_TIMESTAMP = @ACCESS_TIMESTAMP
IF @@ROWCOUNT = 0
BEGIN
ROLLBACK TRAN
SELECT 'NSIR: ' + CAST(@NSIR_ID AS NVARCHAR(5)) + ' has changed since
you opened it, so your changes could not be recorded. Please close
this window and select the record again to get the latest changes.'
END
ELSE
COMMIT TRAN
----------------------------------
All updates are still executing though. I tried to make this thing
throw the error and it didn't. It seems like @@ROWCOUNT is never 0.
Hi Alex,
I tried it, but I had no problems. Here is the code I used.
First, the part to set up the table and procedure. Note that I simply
copied your stored procedure; the only change was to remove the owner
(ajamroz.) in fronn of the table name.
drop table IT_NSIR
go
create table IT_NSIR
(ID int NOT NULL,
PRIMARY KEY (ID),
PRODUCT_DESCRIPTION nvarchar(255) NOT NULL,
ACCESS_TIMESTAMP timestamp NOT NULL)
go
drop proc UpdateProc
go
create proc UpdateProc
(
@NSIR_ID INT,
@PRODUCT_DESCRIPTION NVARCHAR(255),
@ACCESS_TIMESTAMP TIMESTAMP
)
AS
BEGIN TRAN
-- UPDATE ajamroz.IT_NSIR
UPDATE IT_NSIR
SET PRODUCT_DESCRIPTION = @PRODUCT_DESCRIPTION
WHERE ID = @NSIR_ID AND ACCESS_TIMESTAMP = @ACCESS_TIMESTAMP
IF @@ROWCOUNT = 0
BEGIN
ROLLBACK TRAN
SELECT 'NSIR: ' + CAST(@NSIR_ID AS NVARCHAR(5)) + ' has changed since
you opened it, so your changes could not be recorded. Please close
this window and select the record again to get the latest changes.'
END
ELSE
COMMIT TRAN
go
insert IT_NSIR (ID, PRODUCT_DESCRIPTION)
select 1, 'Test row'
go
With the table and stored procedure created, I executed this code from
Query Analyzer window #1:
-- Check starting information
select * from IT_NSIR
go
declare @NSIR_ID INT,
@PRODUCT_DESCRIPTION NVARCHAR(255),
@ACCESS_TIMESTAMP TIMESTAMP
-- Choose a row to operate on
set @NSIR_ID = 1
-- Simulate the read
select @PRODUCT_DESCRIPTION = PRODUCT_DESCRIPTION,
@ACCESS_TIMESTAMP = ACCESS_TIMESTAMP
from IT_NSIR
where ID = @NSIR_ID
-- Simulate "human activity"
waitfor delay '00:00:15' -- 15 seconds delay
set @PRODUCT_DESCRIPTION = 'Updated info'
-- Try the update
exec UpdateProc @NSIR_ID,
@PRODUCT_DESCRIPTION,
@ACCESS_TIMESTAMP
-- Review the result
select * from IT_NSIR
During the 15 seconds delay, I switched to Query Analyzer window #2 and
executed this code (to simulate an update by another user):
UPDATE IT_NSIR
SET PRODUCT_DESCRIPTION = 'Updated by someone else'
WHERE ID = 1
After the 15 seconds delay were finished, the output in window #1 was:
ID PRODUCT_DESCRIPTION ACCESS_TIMESTAMP
----------- ---------------------------- ------------------
1 Test row 0x000000000000721D
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
NSIR: 1 has changed since
you opened it, so your changes could not be recorded. Please close
this window and select the record again to get the latest changes.
ID PRODUCT_DESCRIPTION ACCESS_TIMESTAMP
----------- ---------------------------- ------------------
1 Updated by someone else 0x000000000000721E
I then re-executed the query in window #1, but this time I let it run
without executing any other query, The results were:
ID PRODUCT_DESCRIPTION ACCESS_TIMESTAMP
----------- ---------------------------- ------------------
1 Updated by someone else 0x000000000000721E
ID PRODUCT_DESCRIPTION ACCESS_TIMESTAMP
----------- ---------------------------- ------------------
1 Updated info 0x000000000000721F
(Note: I have edited the output above a little - I have removed most of
thhe white space of the product_description column to improve
readability)
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)