I have a table in SQL Server 2008 R2 that holds “positions” for different companies.
I have a function GET_KEY that creates a position number by performing a SELECT MAX over the table. [I can not use Identity Increment because the position number needs to be the first available number for a particular company]
SELECT @v_position_no = max(POSITION_NO) + 0.01
FROM POSITIONS WHERE COMPANY_NO = @p_company_no
I have a function that copies a position COPY_POS.
I want to copy an existing position twice, but to assign each position an appropriate position number.
I run GET_KEY, then COPY_POS, then GET_KEY, then COPY_POS
However, the positions table locks. I am almost positive (and it is logical) that this is because there a conflict between GET_KEY and COPY_POS. [The problem only occurs if I try and do GET_KEY and COPY_POS twice in succession]
What I tried…
-
I tried encasing each of
GET_KEYandCOPY_POSin aBEGIN TRANandCOMMIT TRAN, but this didn’t work. -
The entire stored procedure is contained in a
BEGIN TRYandEND TRYand I tried removing this, but this also didn’t work…
Does anyone have any ideas?
Thank you!
Simply add a
WITH (nolock)to the Select statement!