나는 테이블은 dbo.Tbl_ActivityInformations
라고하고 데이터를SQL 쿼리 확인 조건
Activityid activitymaxcount Activityusedcount
1 10 9
2 10 7
3 15 15
처럼 그리고 다른 테이블은 Tbl_AttendeeInformations
라는 데이터는 Tbl_AttendeeInformations
테이블 데이터에서
AttendedID AssID ActivityID
13 123456 1,2
14 123457 1,3
처럼 페이지의 새 행으로 삽입되고 에 대해 ActivityInformations
테이블 Activityusedcount
열이 1 씩 증가합니다.
이제 AttendeeInformations
에 행을 삽입하기 전에 을 사용하여 Activityusedcount < activitymaxcount
을 확인하고 싶습니다. 조건이 충족되면 롤백해야만 그렇지 않으면 삽입 할 수 있습니다. SplitString
이라는 이름의 함수가 ActivityID
인 Tbl_AttendeeInformations
에 나뉘어 있습니다.
이
은SplitString
create FUNCTION dbo.SplitString(@FormattedString varchar(8000),@Delimitter char(1))
returns @retResults TABLE(Value varchar(8000),Rownumber int)
as
BEGIN
DECLARE @SearchString as varchar(8000)
DECLARE @AssignString as varchar(8000)
DECLARE @Index int
DECLARE @Count int
set @SearchString = @FormattedString
set @AssignString= ''
set @Count = 0
while(len(@SearchString) > 0)
begin
SET @Index = CHARINDEX(@Delimitter,@SearchString, 0)
set @Count = @Count + 1
if @Index = 0
begin
INSERT INTO @retResults
values(@SearchString,@Count)
set @SearchString = ''
continue
end
set @AssignString = SUBSTRING(@SearchString,1, @Index - 1)
INSERT INTO @retResults values
(@AssignString,@Count)
SET @SearchString = (select SUBSTRING(@SearchString, @Index + 1, LEN(@SearchString) - @Index))
end
return
END
마십시오 도움 코드입니다.
을 당신이해야 ** 정상화 ** 제대로 테이블 - 하나의 컬럼에서 '분명히 위반도 제 1 정규형을 1,2'를 가진 -이 그것을 만들 것입니다 정말 열심히하고 지저분한 심지어 가장 기본적인 관계 작업을 수행 ... –
불행한 나는 @ marc_s의 의견을 열 번 upvote 수 없습니다. 첫 번째 정규 형식을 벗어나는 XX로 구분 된 열 값은 끔찍합니다. 키 열에 넣어두면 완전히 받아 들일 수 없습니다. (예, ** ** 당신이 그것을 디자인하지 않았다는 것을 알고 있습니다 ** ** 당신은 그것을 바꿀 수 없습니다 ...하지만 어딘가에 선을 그어야합니다!) –