sql >> Database teknologi >  >> RDS >> Sqlserver

Læs XML-data ind i hierarkiske tabeller

Vi kan (ab)bruge ROW_NUMBER() for at generere ID'erne uden for XQuery. Præambel:

WITH questions AS (
    SELECT
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS ID,
        n.q.value('(./questionText)[1]', 'nvarchar(max)') AS questionText,
        n.q.value('(./result)[1]', 'nvarchar(50)') AS result,
        n.q.query('answer') AS answers
    FROM
        @xml.nodes('/quizresult/question') AS n (q)
), questions_and_answers AS (
    SELECT ID, questionText, result, answer.query('.') AS answer
    FROM questions
    CROSS APPLY answers.nodes('answer') AS a(answer)
)

Og hent nu spørgsmålene med

SELECT ID, questionText, result 
FROM questions

Og svarene med

SELECT ID AS questionID,
    q.answer.value('answer[1]', 'nvarchar(max)') AS answer,
    q.answer.value('answer[1]/@number', 'int') AS number,
    q.answer.value('answer[1]/@value', 'int') AS val,
    q.answer.value('answer[1]/@chosen', 'bit') AS chosen
FROM questions_and_answers AS q


  1. Kan MySqlBulkLoader bruges sammen med en transaktion?

  2. PostgreSQL-kodningsproblem under udførelse af forespørgsel fra kommandolinjen

  3. Oracle SQL returkolonne beregnet ud fra eksisterende kolonner

  4. Kan ikke parse Oracle-tidsstempel i C#