I am reading an xml file in a stored procedure to insert data into an existing table. I have read the xml in a small test to see if I can read it like I think should be able to and it does. I now want to create a stored procedure that inserts data from xml in a 'Question' table and also an 'Answer' table. The thing is I need the primary key from the question table that was just created to insert into the Answer table or else it causes a problem because of the FK.
I have this:
insert into Questions(Question_Text, Questionaire_ID, QuestionType, Filter)
select
X.xmlData.query('Question').value('.', 'varchar(100)') Question_Text,--Question Text
X.xmlData.query('QuestionaireID').value('.', 'varchar(100)') Questionaire_ID,-- Questionaire ID
X.xmlData.query('Type').value('.', 'varchar(100)') WuestioneType,--Question TYPE
X.xmlData.query('Filter').value('.', 'varchar(100)') Filter--Filter
from
(select
cast(x as XML)
from openrowset(bulk 'C:\sqlXML.xml', single_blob) as T(x)
) as T(x)
cross apply
x.nodes('data/New') as X(xmlData);
select @newQuestionId = SCOPE_IDENTITY() <--I need this to know the current
questionID that relate to the following possible answers
insert into Possible_Answers(Question_ID, Possible_Answer_Text,
Explanation_Required, Review_Required, Question_Type)
select @newQuestionId ,
X.xmlData.query('AnswerChoice[1]').value('.', 'varchar(100)') Possible_Answer_Text,
X.xmlData.query('AnswerChoice[2]').value('.', 'varchar(100)') Possible_Answer_Text,
X.xmlData.query('AnswerChoice[3]').value('.', 'varchar(100)') Possible_Answer_Text,
X.xmlData.query('AnswerChoice[4]').value('.', 'varchar(100)') Possible_Answer_Text,
X.xmlData.query('ExplanationRequired').value('.', 'varchar(100)') Explanation_Required,
X.xmlData.query('ReviewRequired').value('.', 'varchar(100)') Review_Rewuired
from (
select cast(x as XML)
from openrowset(
bulk 'C:\sqlXML.xml',
single_blob) as T(x)
)
as T(x)
cross apply x.nodes('data/New') as X(xmlData);
Now this could be all wrong because in my test I just read the data and didn't insert it into a table.
And this is my XML:
<?xml version="1.0" encoding="UTF-8" ?>
<data>
<New>
<QuestionaireID>2</QuestionaireID>
<Type>1</Type>
<Question>Does this test work?</Question>
<Filter>31</Filter>
<AnswerChoice>true</AnswerChoice>
<AnswerChoice>false</AnswerChoice>
<ExplanationRequired></ExplanationRequired>
<ReviewRequired></ReviewRequired>
</New>
<New>
<QuestionaireID>3</QuestionaireID>
<Type>2</Type>
<Question>Does this test work really?</Question>
<Filter>127</Filter>
<AnswerChoice>answer A</AnswerChoice>
<AnswerChoice>Answer B</AnswerChoice>
<AnswerChoice>Answer C</AnswerChoice>
<AnswerChoice>Answer D</AnswerChoice>
<ExplanationRequired></ExplanationRequired>
<ReviewRequired></ReviewRequired>
</New>
</data>
As you can see there may be question with on 2 possible answers or 4 possible answers...
I get the errors:
An INSERT statement cannot contain a SELECT statement that assigns values to a variable.
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
I know where the errors are coming from but I was wondering if there was a way to do this?