sql >> データベース >  >> RDS >> Sqlserver

XML データを階層テーブルに読み込む

    ROW_NUMBER() を (ab) 使用できます XQuery の外部で ID を生成します。前文:

    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)
    )
    

    そして、

    で質問を取得します
    SELECT ID, questionText, result 
    FROM questions
    

    そして

    での答え
    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. SQLクエリのパラメータ名を定義する必要があります

    2. trunc(date、'IW')とは正確には何ですか?

    3. MYSQL検索でのURLパラメータの使用

    4. 全文検索では、テストとライブで異なる結果が返されます