Author: way0utwest

  • Parsing SQL Saturday Data – Getting Titles from the XML document

    I’m continuing on with my project to grab SQL Saturday data and automatically insert it into a SQL Server database. In this piece, I’m picking up from the last one where I had a loop to load all XML documents in a folder based on a pattern.

    This time I want to query the XML and get out specific elements and capture them.

    The Source

    The XML source looks like this for the sessions:

    </event>
    <event>
      <importID>2102</importID>
      <speakers>
        <speaker>
          <id>2102</id>
          <name>Jason Strate</name>
        </speaker>
      </speakers>
      <track>Track 3</track>
      <location>
        <name>2520C (Conference room)</name>
      </location>
      <title>Using XML to Query Execution Plans </title>
      <description>SQL Server stores its execution plans as XML in dynamic management views. The execution plans are a gold mine of information. From the whether or not the execution plan will rely on parallelism to what columns are requiring a key lookup after a non-clustered index seek. Through a the use of XML this information can be available at your fingertips to help determine the value and impact of an index and guide you in improving the performance of your SQL Server databases. In this session we’ll look at how you can begin to understand and query the structure of the execution plans in the procedure cache. Also, we’ll review how to uncover some potential performance issues that may be lurking in your SQL Server.</description>
      <startTime>9/18/2010 12:15:00 PM</startTime>
      <endTime>9/18/2010 1:30:00 PM</endTime>
    </event>
    <event>
      <importID>2109</importID>
      <speakers>
        <speaker>
          <id>2109</id>
          <name>Jason Strate</name>
        </speaker>
      </speakers>
      <track>Track 4</track>
      <location>
        <name>2520D (Seminar room)</name>

    I’m showing the end of one element, one whole one, and the start of a third. There is a lot of extraneous information in the document that I don’t want (for now). As a result, it’s not as simple to query this as I’d thought before. Especially as I’ll want to capture each session title and insert it into a database.

    I decided to use a SelectNodes to get to the <event> nodes and then loop through them. The code looks like this:

    # get the event node
    $sessions = $doc.SelectNodes("//event")

    # loop through the various //event nodes
    foreach ($session in $sessions) {

    Note that this is inside of the code from the previous post.

    Inside of this loop, I decided to create another loop. Initially I didn’t, but that made it more difficult to determine the end of the event node and capture the values, especially the speakers. As a result, I have a sub loop at well:

    # probably a better way, but I wanted to loop through the various elements and only pick out certain ones
    foreach ($detail in $session.ChildNodes) {

    If anyone has a better way, let me know. I’ll have all the code below, but this technique allows me to look for specific nodes. I know I could query for them, but since I’m looking for a few specific items, I thought I’d do this rather than multiple queries later.

    Get the Title

    I actually need the title and the speaker child node, but I’m doing titles only here. Here’s the whole node loop code:

    foreach ($detail in $session.ChildNodes) {

      # If we’re on the title node, get the value
      if ($detail.Name -eq "title") {
        $title = $detail.’#text’
       }

      if ($detail.Name -eq "speakers") {
        #placeholder
       }
    #end foreach for $detail
    }

    Here if I have the title element in the foreach loop, I capture it. This allows me to use this variable later. I’ll go into the speaker code later, but for now, I left a placeholder.

    That’s really it. At the end of the outer foreach, I write out the $event and $title variables. This gives me a nice output to the screen. From here I can easily substitute some ADO code to send this to SQL Server instead of the write-host, but that’s a good programming technique for me to see if I’ve got the data I want.

    sqlsatloop_d

    As you can see, there are sessions that I don’t want, but there’s nothing in the data for me to tag them as non-educational sessions. I’m not sure I care, since the speakers associated with these won’t impact my results for reports, so I’ll leave them.

    Next Steps

    From here I need to extract the speakers before I insert data into SQL Server. That will be the next step before I create the database and then insert data.

    The Code

    Here’s the entire code:

    #ViewXML_Basic
    # View XML file data from a website

    $debug = 0;
    # counter for events
    $i = 1

    #when do we stop?
    $loopend = 400
    $baseURL = "E:\SQLSatData\SQLSat"
    $loop = 1
    $doc = New-Object System.Xml.XmlDocument

    do {
    #start large loop

      # get the filename
      $sourceURL = $baseURL + $i + ".xml"

      # debug information
      if ($debug -eq 2) {
        Write-Host $sourceURL
        }

      #test the path first. If it exists, load the XML
      if (Test-Path $sourceURL) {
        $doc.Load($sourceURL)

        #trap the event number. This will be the ID I use in the database table.
        $event = "SQL Saturday #" + $i

        # get the event node
        $sessions = $doc.SelectNodes("//event")

        # loop through the various //event nodes
        foreach ($session in $sessions) {
     
        # probably a better way, but I wanted to loop through the various elements and only pick out certain ones
        foreach ($detail in $session.ChildNodes) {

          # If we’re on the title node, get the value
          if ($detail.Name -eq "title") {
            $title = $detail.’#text’
           }

          if ($detail.Name -eq "speakers") {
            #placeholder
           }
         #end foreach for $detail
         }

        write-host $event ": " $title
       
        # placeholder – insert into table here. $i, $title

        $title = ""
        $speakers = ""

       #end foreach for $sessions   
       }

       # end test path
       }
      # increment loop
      $i++

    #end outer loop
    } while ($i -lt $loopend)

    write-host "end"

  • Small Leadership

    I ran across Richard Branson’s top quotes on leadership. From a man who’s been quite a success in many areas, and has influenced many others, it was interesting to see what he considers good advice. He lists ten quotes from various others, some of which I’ve heard before. It’s an interesting mix, and these are certainly items that come from someone that could be considered a leader in business. I don’t know if that’s true, but certainly Mr. Branson is in charge of his company, and I believe that it is important for the management of a company to display leadership if they want to be successful.

    Most of us, however, aren’t in management positions. Most of us might not want to ever be in management positions. Plenty of technical people prefer to remain in their roles as developers, DBAs, analysts and more. However, that desire doesn’t mean that better leadership skills wouldn’t make our jobs easier, or make us more successful.

    The first quote in the story is this: “A lot of people have gone further than they thought they could because someone else thought they could.” That’s the quote of leadership that inspires, that helps others gain confidence and do more than they might otherwise. That’s the quote that shows a person who helps others is a great employee, not only because they get work done, but because they help others perform better.

    Far too many managers don’t believe in their staff, and make the clear constantly. Those managers aren’t helping themselves or their company, as they’ll ensure most of their employees don’t do the best work they can. I’d encourage all of you to think about how you treat others. Think about what you say or do to encourage others to become better at their jobs. Perhaps there’s a little more you can to do lead others, and get more work done, while celebrating the success you can have together as a group of professionals.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 2.1MB) podcast or subscribe to the feed at iTunes and LibSyn.

  • DLM Workshops

    We’ve got a bunch of DLM workshops coming up in the next few months from Red Gate Software. We’ve been working on trying to bring some depth to our education, helping DBAs and developers learn how to built smoother software development pipelines for their databases.

    The list of workshops is at http://www.red-gate.com/training/workshops/ and we’ll be updating it throughout the year. In this series, I’ll be at these events

    • Mar 10 – Dublin
    • Apr 10 – LA

    Certainly more are coming later in the year that I’ll deliver or help out at.

    We’ve built a series of three days of in depth instruction, each one building on the next. Our hope is to help you move through the subjects. Right now the days are covering these topics.

    • Version Control for Databases
    • Continuous Integration
    • Automating Deployments

    We are looking to try and schedule these over time in a city, so that you’ll be able to learn about VCS for databases, then come back in 3-4 months and take the next step to CI. We might do all three days consecutively, but it’s a lot to learn and cover, and I’d really hope that companies would learn one area, implement it, and then come back to learn the next step.

    If you have requests for dates or cities, or even private instruction, let us know. We’re growing a list of partners as well because Grant and I can’t be everywhere, so we hope to be able to help more people learn ways to build software smoother and quicker.

  • Making Plans

    I write regularly about career topics, hoping that I can help many of you think about your career more, learn more, and move in a direction that’s important to you. I’ve had a great career, one I’ve enjoyed most of the time, and think that pressing myself to become a better professional in many ways has been a large part of that. However I haven’t always had a formal plan, and don’t really have any long term goals in any detail.

    I thought about this a bit in reading Kenneth Fisher’s interview of Tom LaRock. There’s a point where Ken asks Tom about his future plans and Tom paraphrases John Lennon to say he takes opportunities as they come and isn’t afraid to change directions in life.

    As important as I think it is to continue to learn, and to move your career forward actively, I don’t think you need to do it all the time. I don’t think it’s a constant, every day effort that you make. There are times when you’re busy with life, or busy with work, and you may not have the extra energy or desire to pick up a book or write some T-SQL. That’s fine. Relax and enjoy your life.

    Just don’t let those times stretch so far that your career stagnates. Make regular progress, not constant progress. Make plans, but live life as it passes. Make goals, but make them realistic, and pace yourself. Above all, don’t be afraid to change if that’s what’s right for you.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 2MB) podcast or subscribe to the feed at iTunes and LibSyn.