我在Intranet上有一個頁面,它將請求提交給perl CGI腳本。該腳本依次調用SQL Server數據庫上的存儲過程來檢查是否存在具有某些特性的對象。如果確實如此,storproc將返回樂器的ID,如果不是,則會創建一個新樂器並返回該樂器的ID。存儲過程創建一個事務,並在插入語句中使用with (TABLOCKX)
。爲了用戶友好,當所述用戶同時提交一堆請求時,網頁將請求異步地提交給perl腳本。我認爲當提交幾個請求都需要一個新的工具時,第一個打到storproc的請求將運行,鎖定表,創建新工具,釋放鎖,然後對storproc的後續調用將會知道新儀器並使用它。我在實踐中看到的是,會有一些請求創建新的工具,其餘的將使用最新的工具。我嘗試在客戶端使用setTimeout來分隔請求,但這似乎沒有什麼區別。任何想法,我可能做錯了什麼?從存儲過程中插入SQL Server獨特行不起作用
這裏是存儲過程的代碼:
CREATE PROCEDURE [dbo].[CreateFutures]
@code varchar(5),
@month int,
@year int,
@currency varchar(3)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION
declare @ticker varchar(7)
declare @yearCode char(1)
declare @res as Table (id int)
declare @n as int
set @yearCode = convert(char(1), @year % 10)
set @ticker = (
select @code + futures + @yearCode
from FuturesMonthCodes
where month = @month
)
insert into @res
select top 1 instrument
from InstrumentFutures // This is a view that joins InstrumentText and InstrumentNumber data
where ticker = @ticker
and code = @code
and month = @month
and year = @year
and currency = @currency
order by instrument
set @n = (select COUNT(id) from @res)
if @n = 0
begin
print 'Creating Future'
declare @id int
declare @stamp datetime
set @stamp = CURRENT_TIMESTAMP
insert into Instrument with (TABLOCKX) (insertTime) values (@stamp)
set @id = (select SCOPE_IDENTITY());
insert into InstrumentText (instrumentId, name, value) values (@id, 'type', 'futures')
insert into InstrumentText (instrumentId, name, value) values (@id, 'ticker', @ticker)
insert into InstrumentText (instrumentId, name, value) values (@id, 'code', @code)
insert into InstrumentText (instrumentId, name, value) values (@id, 'currency',@currency)
insert into InstrumentNumber (instrumentId, name, value) values (@id, 'month', @month)
insert into InstrumentNumber (instrumentId, name, value) values (@id, 'year', @year)
insert into @res (id) values (@id)
end
commit transaction
if @n = 0 --instrument created
select top 1 id, 1 from @res order by id
else --returning existing instrument
select top 1 id, 0 from @res order by id
END
要開始,發佈插入過程的代碼。 – 2012-02-02 23:46:12