예를 들어 볼 때 거래가 사용중인 것처럼 보이지 않습니다. 내 생각 엔 성명의 SELECT 부분이 로 표시되어 있습니다. 읽지 않은 경우을 읽으십시오. 그렇지 않으면 중복이 표시되지 않습니다. ADO와 트랜잭션을 시작하는 방법이 있지만 저장 프로 시저를 대신 사용하는 것이 좋습니다. 이런 식으로 뭔가를 구현
시도 :이 나중에 작동하지 않는 경우
CREATE PROC dbo.ResponseHeader_Insert
<more data to insert>,
@ProjectID INT,
@Status SMALLINT
as
insert responseheader (column names here)
select <param values here>, isnull(max(rhSerial), 0) + 1
from responseheader
where (rhstatus = @Status) AND (rh_projectID = @ProjectID))
는, 시퀀스 테이블을 생성 (각 시퀀스를)보십시오. 가 생성 될 필요가 너무 많은 순서 테이블 또는 당신이 즉석에서 시퀀스를 만들려면
이
create procedure GetNewSeqVal_<tablename>
as
begin
declare @NewSeqValue int
set NOCOUNT ON
insert into <tablename> (SeqVal) values ('a')
set @NewSeqValue = scope_identity()
delete from <tablename> WITH (READPAST)
return @NewSeqValue
end
, 시도이 방법을 :
create table <tablename> (
SeqID int identity(1,1) primary key,
SeqVal varchar(1)
)
다음 정체성을받을 수있는 프로 시저를 작성
을
Create table AllSequences (
SeqName nvarchar(255) primary key, -- name of the sequence
Seed int not null default(1), -- seed value
Incr int not null default(1), -- incremental
Currval int
)
Go
create procedure usp_CreateNewSeq
@SeqName nvarchar(255),
@seed int = 0,
@incr int = 1
as
begin
declare @currval int
if exists (
select 1 from AllSequences
where SeqName = @SeqName)
begin
print 'Sequence already exists.'
return 1
end
if @seed is null set @seed = 1
if @incr is null set @incr = 1
set @currval = @seed
insert into AllSequences (SeqName, Seed, Incr, CurrVal)
values (@SeqName, @Seed, @Incr, @CurrVal)
end
go
create procedure usp_GetNewSeqVal
@SeqName nvarchar(255)
as
begin
declare @NewSeqVal int
set NOCOUNT ON
update AllSequences
set @NewSeqVal = CurrVal = CurrVal+Incr
where SeqName = @SeqName
if @@rowcount = 0 begin
print 'Sequence does not exist'
return
end
return @NewSeqVal
end
go
'rhserial' 컬럼을'INT IDENTITY' 컬럼으로 만들고 순차 번호를 SQL 서버로 늘리면 안 될까요? 이것이 가장 좋은 방법입니다 - 번거롭고 번거롭지 않습니다. SQL 서버가 –
으로 작업하게하십시오. 각 프로젝트마다 고유 한 일련 번호가 필요하기 때문입니다. 따라서 프로젝트 1234는 1에서 위로 rhserial을가집니다. 그리고 1235 프로젝트는 1에서 위로 rhserial을 가질 것입니다. rhserial은 테이블에서 고유하지 않습니다. – derekcohen
그런 경우, (1) 각 프로젝트의 현재 일련 번호가있는 테이블과 (2) 각 프로젝트의 다음 유효한 번호를 가져 오는 스레드 및 동시성 안전 메소드 (예 : 저장 프로 시저)가 있어야합니다. 그 "serial-no table". 'SELECT MAX() + 1'을 사용하는 것만으로도 ** 안전하지 않습니다 **. [이렇게 다른 방법을 보여주는 Remus Rusanu의 다른 질문을 참조하십시오] (http://stackoverflow.com/questions/5083846/sql-server-2005-using-generated-sequences-instead-of-identity-columns) –