I have recently implemented SQL rowversion
to prevent concurrency issues in my system. I use rowversion
in where clause when updating single rows in the tables. So far I have tested and seems like a good solution. Now I'm looking for an easy way to implement this feature in my system. Here is SP that runs when user wants to update the record:
CREATE PROCEDURE [dbo].[UpdateBuilding]
@Status BIT = NULL,
@Name VARCHAR(50) = NULL,
@Code CHAR(2) = NULL,
@OriginalRowVersion ROWVERSION
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN
UPDATE dbo.Building
SET Status = @Status,
Name = @Name,
Code = @Code,
ActionDt = CURRENT_TIMESTAMP
WHERE RowVersion = @OriginalRowVersion
IF @@ROWCOUNT = 0
BEGIN
RAISERROR('Buildingwith code %s was modified or deleted by another user.', 16, 1, @Code);
END;
END;
If I want to execute SP above I would need to pass required parameters. This is how I call SP in SQL Management Studio:
EXEC UpdateBuilding
@Status = 1,
@Name = "Rockefeller Center",
@Code = 436,
@OriginalRowVersion = 0x0000000000006955;
Now I started looking how to implement this in my system where I use ColdFusion to communicate with Datatbase. Here is example on how this procedure will be executed with CF 2016:
<cfstoredproc procedure="UpdateBuilding" datasource="#dsn#">
<cfprocparam dbvarname="@Status" value="#trim(arguments.status)#" cfsqltype="cf_sql_bit" />
<cfprocparam dbvarname="@Code" value="#trim(arguments.code)#" cfsqltype="cf_sql_char" maxlength="2" null="#!len(trim(arguments.code))#" />
<cfprocparam dbvarname="@Name" value="#trim(arguments.name)#" cfsqltype="cf_sql_varchar" maxlength="50" null="#!len(trim(arguments.name))#" />
<cfprocresult name="Result"/>
</cfstoredproc>
You can see that all values are passed with arguments that user submits in the form. However, updating based on PK
value (Code column in my case) was pretty simple. Now I have binary value and that makes everything more complicated. First I use JSON to send the data to client side. Sending rowversion
in JSON object would require converting that value to binary
and then converting back when user submits the form. I'm wondering if there is better way to achieve this? Ideally I would not even send rowversion
value to the user side. I woul keep that on the back end and once user submits the form pull row version value based on PK then call stored procedure. If anyone knows good way to handle this kind of situations please let me know. I have not used rowversion
before and this is new to me.