Popular Posts

Thursday, June 2, 2011

Insert data in SQL Table from XML




--select * from sp_help  tbl_Web_Fdbk_Answers
--select * from tbl_Web_Fdbk_Feedback

create proc usp_AnswerInsert
@RetrieveXMLDoc xml   
AS
declare @docHandler int
exec sp_xml_preparedocument @docHandler output,@RetrieveXMLDoc
declare @error nvarchar(max)
set @error = '0'
begin tran
    declare @FeedbackID int = null
    declare @QuestionID int = null
    declare @DateSubmitted date = null
    declare @Score int
    declare @chk bit
         --set @InsertId=SCOPE_IDENTITY()
    select @FeedbackID =MAX(FeedbackID ) from tbl_Web_Fdbk_Feedback
   
        select identity(int,1,1)SNo,QuestionID,Score into #tempOpt from openxml (@docHandler,'DocumentElement/Questions',2)
        --select * from #tempOpt
       
            with(QuestionID int,Score int)       
            declare @NewSNo int
            set @NewSNo =1
            select    @QuestionID=QuestionID,@Score=Score from #tempOpt where SNo = @NewSNo
            select @QuestionID
        while @@rowcount > 0
        begin
        select 'Right2'
                Insert into tbl_Web_Fdbk_Answers(FeedbackID,QuestionID,Score,DateSubmitted)Values(@FeedbackID,@QuestionID,@Score,GETDATE())
   
                set @NewSNo = @NewSNo + 1
                select    @QuestionID=QuestionID,@Score=Score from #tempOpt where SNo = @NewSNo   
        end
        commit tran
--if @error = '0'
--begin
--    select 'Right'
--    commit tran
--end
--else
--begin
--    select 'Fail'
--    rollback tran
--end



--===========
usp_AnswerInsert '<DocumentElement>
  <Questions>
    <QuestionID>1</QuestionID>
    <Score>2</Score>
  </Questions>
  <Questions>
    <QuestionID>2</QuestionID>
    <Score>4</Score>
  </Questions>
  <Questions>
    <QuestionID>3</QuestionID>
    <Score>6</Score>
  </Questions>
  <Questions>
    <QuestionID>4</QuestionID>
    <Score>8</Score>
  </Questions>
  <Questions>
    <QuestionID>5</QuestionID>
    <Score>6</Score>
  </Questions>
  <Questions>
    <QuestionID>6</QuestionID>
    <Score>4</Score>
  </Questions>
</DocumentElement>'

No comments:

Post a Comment