저는 현재 저장 프로 시저 디자인을 많이하지 않았기 때문에 당황한 프로젝트를 진행하고 있습니다. 사용자를 입력 할 때 하나 이상의 그룹과 연결될 수있는 작업이 있습니다. 저장된 proc을 @GroupList = " 'Group1', 'Group2', 'Group3'"로 호출 할 때 그룹 목록을 매개 변수로 전달하고 있습니다. 여러 그룹이있을 수 있습니다. 그룹 중 하나가 존재하지 않으면 새 사용자 추가 호출이 실패해야합니다. 기존 그룹의 ID로 임시 테이블을 채우기 전에 다음 코드를 사용하여 존재하는지 확인하려고합니다. 내 문제는 계속 실패한다는 것입니다. 내가 생각할 수있는 모든 것을 시도해 보았 기 때문에 나보다 더 실력있는 사람들에게 의지하고 있습니다. 미리 감사드립니다. 나에게 실패하는 것은 if exist 문입니다.
Declare @GroupList varchar(100), @return int, @sql varchar(1000)
if @GroupList is not null
begin
if (object_id ('tempdb..#TempGroupList') is not null) drop table #TempGroupList
Create Table #TempGroupList
(
GroupID int,
PlayerID int
)
if exists (select * from Group_new where GroupName in (' + @GroupList + '))
begin
--lets populate our temp table
set @SQL = 'insert into #TempGroupList (GroupID) select GroupID from Group_New where GroupName in (' + @GroupList + ')'
exec(@sql)
end
else
set @return = 1
select @return
print 'Stop, one of the Group names you entered does not exist'
Return
end
입력을 분리하고 확인한 다음 삽입합니다.
create procedure . . . (
Declare @GroupList varchar(100),
@return int, @sql varchar(1000)
) as
begin
declare table @groups (grp varchar(255));
insert @groups (grp)
select ltrim(rtrim(value))
from string_split(@grouplist, ',');
if not exists (select 1
from @groups g
where not exists (select 1 group_new gn where gn.groupname = g.groupname)
)
begin
. . . whatever you want to do here
end;
else begin
print 'Stop, one of the Group names you entered does not
end;
end;
저장 프로 시저에 정의 된 임시 테이블을 채우는 것은 저장 프로 시저가 반환 될 때 임시 테이블이 삭제되기 때문에 많은 작업을 수행하지 않습니다. 그러나 원하는 논리를 다음 부분에 넣을 수 있습니다 if
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다