如何循环XML类型并在SQL Server中每次迭代插入记录?

时间:2017-08-24 12:37:19

标签: sql-server xml

我想创建一个具有 public class MainVerticle extends AbstractVerticle { @Override public void start(Future<Void> fut) { //your logic goes here for message object to pass MessageConsumer<Object> mc = vertx.eventBus().consumer("your_addressToServer").handler(message -> { Object message = message.body(); final DeliveryOptions options = new DeliveryOptions().setCodecName("any_messaging_address_name"); vertx.eventBus().send("any_messaging_address_name", Object(message object to pass) , options); }); } } 参数的存储过程,该参数充当我在应用程序中构造的XML字符串的容器。我设法提出了这个xml字符串,它将作为public class MessagingVerticle extends AbstractVerticle { @Override public void start(Future<Void> fut) { vertx.eventBus().<Object>consumer("any_messaging_address_name", message -> { Object messageCommand = message.body(); // received object from MainVerticle }); fut.complete(); } } 参数传递给存储过程,并将转换为XML:

varchar(max)

我有varchar(max)<SurveyQuestion> <Name>What is your pets name?</Name> <Type>1</Type> <IsRequired>True</IsRequired> <Answer></Answer> </SurveyQuestion> <SurveyQuestion> <Name>What is your gender?</Name> <Type>3</Type> <IsRequired>True</IsRequired> <Answer>Male</Answer> <Answer>Female</Answer> <Answer>Trans</Answer> </SurveyQuestion> <SurveyQuestion> <Name>Which colors do you like?</Name> <Type>4</Type> <IsRequired>False</IsRequired> <Answer>Yellow</Answer> <Answer>Green</Answer> <Answer>Red</Answer> <Answer>Blue</Answer> <Answer>Orange</Answer> </SurveyQuestion> <SurveyQuestion> <Name>Rate the service that you have receive from 1 to 5. I being the lowest and 5 being the highest</Name> <Type>2</Type> <IsRequired>True</IsRequired> <Answer>1</Answer> <Answer>2</Answer> <Answer>3</Answer> <Answer>4</Answer> <Answer>5</Answer> </SurveyQuestion> 表:

Questions

这是我的存储过程:

Answers

1 个答案:

答案 0 :(得分:1)

如果Name在您的XML中是唯一的,则可以使用两个INSERT和临时表来执行此操作:

CREATE TABLE #Questions
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Question] [varchar](max) NULL --Name
)

CREATE TABLE #Answers
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [QuestionId] [bigint] NULL, --Foreign key Questions
    [Options] [varchar](50) NULL --Answer
)

declare @xml xml = '<root>
<SurveyQuestion>
    <Name>What is your pets name?</Name>
    <Type>1</Type>
    <IsRequired>True</IsRequired>
    <Answer></Answer>
</SurveyQuestion>
<SurveyQuestion>
    <Name>What is your gender?</Name>
    <Type>3</Type>
    <IsRequired>True</IsRequired>
    <Answer>Male</Answer>
    <Answer>Female</Answer>
    <Answer>Trans</Answer>
</SurveyQuestion>
<SurveyQuestion>
    <Name>Which colors do you like?</Name>
    <Type>4</Type>
    <IsRequired>False</IsRequired>
    <Answer>Yellow</Answer>
    <Answer>Green</Answer>
    <Answer>Red</Answer>
    <Answer>Blue</Answer>
    <Answer>Orange</Answer>
</SurveyQuestion>
<SurveyQuestion>
    <Name>Rate the service that you have receive from 1 to 5. I being the lowest and 5 being the highest</Name>
    <Type>2</Type>
    <IsRequired>True</IsRequired>
    <Answer>1</Answer>
    <Answer>2</Answer>
    <Answer>3</Answer>
    <Answer>4</Answer>
    <Answer>5</Answer>
</SurveyQuestion>
</root>';

CREATE TABLE #temp 
(
    [Id] [bigint] NOT NULL,
    [Question] [varchar](8000) NOT NULL, --Name, UNIQUE!!!
    CONSTRAINT UC_Question UNIQUE(Question)
);

insert into #Questions (Question)
    output INSERTED.Id, INSERTED.Question
    into #temp (id, question)
    select n.value('.', 'varchar(max)') Name
        from @xml.nodes('/root/SurveyQuestion/Name') xml(n);

insert into #Answers (QuestionId, Options)
    select t.Id, n.value('.', 'varchar(50)') answer
        from #temp t
        cross apply @xml.nodes('/root/SurveyQuestion[Name = sql:column("t.question")]/Answer') answers(n);

select * from #Questions;
select * from #Answers;

drop table #Questions;
drop table #Answers;
drop table #temp;