IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_document_answer]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_document_answer]
GO

CREATE proc mps_save_document_answer(
@useruid int ,  
    @docid bigint, 
    @answer varchar(max))
as
begin
set nocount on

declare @s varchar(100);
declare @s1 varchar(100);
declare @s2 varchar(max);
declare @site int;
declare @site2 int;
declare @error varchar(1000) = '';
declare @temp varchar(1000) = '';
declare @i int =0;
declare @j int =0;

if @answer is null or @answer = ''
begin
select 'no question' as result
end
else
begin
while(@answer>'')
begin
set @site = CHARINDEX(',',@answer)
if @site = 0
begin
set @s = @answer
set @answer = ''
end
else
begin
set @s = LEFT(@answer, @site-1);
set @answer = SUBSTRING(@answer,@site+1,LEN(@answer))
end
-- 获取de_id及de_result
set @site2 = CHARINDEX('|', @s)
set @s1 = LEFT(@s, @site2-1);
set @s2 = right(@s, LEN(@s)-@site2);
set @i = @i + 1;
set @temp = '';

select @temp = @s1 from mps_document_exam
where de_id = convert(int,@s1) and de_result = @s2

if(@temp = @s1)
begin
set @j = @j + 1;
end
else
begin
set @error = @s1 + '|'+ @error;
end
end
if (@i=@j)
begin
insert into mps_document_examanswer(dea_useruid,dea_docid,dea_score)
values(@useruid,@docid,100)
select 'ok' as result
end
else
begin 
select left(@error,len(@error)-1) as result
end
end

set nocount off
end