IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_doc_exam]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_doc_exam]
GO
create proc [dbo].[mps_save_doc_exam]
@content nvarchar(4000)
as
begin
    set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
     declare @s nvarchar(4000),@answer varchar(10),@docid varchar(10),@count int;
 set @s = @content;
 --取得问题个数
 select @count = convert(varchar(100),right(@s, len(@s)-CHARINDEX(''',count,''', @s, 1)-8))
 --删除个数，重新赋值
 SET @s = STUFF(@s, CHARINDEX(''',count,''', @s, 1),len(@s)-CHARINDEX(''',count,''', @s, 1)+1 , '');
 --获取docid
 select @docid = right(@s, len(@s)-CHARINDEX(''',docid,''', @s, 1)-8)
 delete from mps_document_exam where de_docid = convert(bigint,@docid)
 --删除docid，重新赋值
 SET @s = STUFF(@s, CHARINDEX(''',docid,''', @s, 1),len(@s)-CHARINDEX(''',docid,''', @s, 1)+1 , '');
 --select @s
     --获取问题答案
 declare @j int,@question nvarchar(1000);
 declare @acount int,@truea varchar(10),@title nvarchar(500);
 set @j=0
 while @j<@count
 begin
     --查询出某个问题全部
     select @question = left(@s, CHARINDEX(''',,,,,''', @s, 1)-1)
 --查询出答案个数
 select @acount = convert(varchar(100),right(@question, len(@question)-CHARINDEX(''',acount,''', @question, 1)-9))
 --删除答案个数
 SET @question = STUFF(@question, CHARINDEX(''',acount,''', @question, 1),len(@s)-CHARINDEX(''',acount,''', @question, 1)+1 , '');
 --查询正确答案
 select @truea = right(@question, len(@question)-CHARINDEX(''',,,''', @question, 1)-4)
 --删除正确答案
 SET @question = STUFF(@question, CHARINDEX(''',,,''', @question, 1),len(@s)-CHARINDEX(''',,,''', @question, 1)+1 , '');
 --查询问题题目
 select @title= left(@question, CHARINDEX(''',,,,''', @question, 1)-1)
 --删除问题题目
 set @question = STUFF(@question,1, CHARINDEX(''',,,,''', @question, 1)+5 , '');
 declare @k int,@params varchar(200),@alla nvarchar(4000);
 set @k =0;
 set @params = '';
 while @k<@acount
  begin 
      if(@k<(@acount-1))
  begin
      set @params = @params+ 'de_option'+convert(varchar(10),(@k+1))+','
  end
  else if(@k=(@acount-1))
  begin
      set @params = @params+'de_option'+convert(varchar(10),(@k+1))
  end
      set @k += 1;
  end 
  --拼接成值
  set @question =''''+ REPLACE(@question,''',,''',''',''')+''''
 declare @allsql nvarchar(4000)
 set @allsql = 'insert into mps_document_exam('+@params+',de_docid,de_title,de_result) values('+@question+','+@docid+','''+@title+''','''+@truea+''')'
 exec (@allsql)
 --删除某个问题全部
 set @s = STUFF(@s,1, CHARINDEX(''',,,,,''', @s, 1)+6 , '');
     set @j += 1;
 end   
 set nocount off
end