Integration of XQuery and T-SQL

  • Hello,

    I'm struggling with the integration of XQuery and T-SQL syntax.

    I found a piece of code which looks for a certain pattern of pitches but I can't seem to find a way to transform the following XQuery code to run with SQL Server.

    # Go to each potential common-ancestor:

    FOR $part in //part

    # and compile a list of its notes:

    LET $notes := $part//note

    # then generate each subsequence of 4 notes:

    FOR $i IN 1 TO count($notes)-3

    LET $four_notes := $notes[$i TO $i+3]

    # and check that the 4 notes have the right pitch:

    WHERE string($four_notes) = ( "C", "D", "E", "C" )

    RETURN $four_notes

    How can I rewrite this to work with TQL?

    Thanks in advance!

  • I think it would be easier for most of us if you'd provide sample data and expected result instead of a code snippet (not even sure what programming language that is - looks like F#...)



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • The language is supposed to be XQuery (I know the keywords shouldn't be in capital letters) 🙂

    My main problem is to transform the loop-construct to work with TSQL-Syntax, since XQuery isn't fully supported within SQL Server 2008.

    OK, here's the code snippet:

    <part id="P1">

    <measure number="2" width="193">

    <note default-x="13">

    <pitch>

    <step>A</step>

    <octave>4</octave>

    </pitch>

    <duration>4</duration>

    <voice>1</voice>

    <type>quarter</type>

    <stem default-y="11">up</stem>

    </note>

    <note default-x="103">

    <pitch>

    <step>F</step>

    <octave>4</octave>

    </pitch>

    <duration>4</duration>

    <voice>1</voice>

    <type>quarter</type>

    <stem default-y="0.5">up</stem>

    </note>

    </measure>

    <!--=======================================================-->

    <measure number="3" width="200">

    <note default-x="13">

    <pitch>

    <step>F</step>

    <octave>4</octave>

    </pitch>

    <duration>6</duration>

    <voice>1</voice>

    <type>quarter</type>

    <dot/>

    <stem default-y="0.5">up</stem>

    </note>

    <note default-x="140">

    <pitch>

    <step>G</step>

    <octave>4</octave>

    </pitch>

    <duration>2</duration>

    <voice>1</voice>

    <type>eighth</type>

    <stem default-y="5.5">up</stem>

    </note>

    </measure>

    </part>

  • tonyclifton77 (9/8/2010)


    The language is supposed to be XQuery (I know the keywords shouldn't be in capital letters) 🙂

    My main problem is to transform the loop-construct to work with TSQL-Syntax, since XQuery isn't fully supported within SQL Server 2008.

    OK, here's the code snippet: (...code snippet remved...)

    I don't think it's an issue of capital letters or not. The problem I had is the different definition of being a valid XQuery statement (it might be valid in plain XQuery syntax, but it's not in terms of XQuery implementation in SQL Server)...

    What would help is your expected output based on the sample code you provided.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • for the expected output I'd be happy with a simple true or false - as in the exist-method.

  • tonyclifton77 (9/8/2010)


    for the expected output I'd be happy with a simple true or false - as in the exist-method.

    What would be your true or false condition?



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • LutzM (9/8/2010)


    tonyclifton77 (9/8/2010)


    for the expected output I'd be happy with a simple true or false - as in the exist-method.

    What would be your true or false condition?

    matching a pattern - in the example CDEC

    WHERE string($four_notes) = ( "C", "D", "E", "C" )

  • Will the following work?

    IF REPLACE(CAST(@xml.query('data(part/measure/note/pitch/step)') AS VARCHAR(10)),' ','') ='AFFG'

    PRINT 'true'

    ELSE

    PRINT 'false'



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply