Category: Blog

  • T-SQL Tuesday #39 – PowerShell

    tsqltuesdayThis month the invitation is from Wayne Sheffield, asking if you have a PowerShell story for everyone. That’s fitting since Wayne is currently writing a month of PowerShell blogs, and they are worth reading.

    The second Tuesday of each month is T-SQL Tuesday, with a new theme each month. If you follow the community on Twitter, you should get an invite every month, and I also keep a history page on my blog that I try to update.

    PowerShell

    I believe in PowerShell. It’s one of the few directives that Microsoft has taken in their products that I think is both a great idea, and one that will last for a long time. As far as I’ve heard, all their future products in the server space need to support Powershell and include hooks to incorporate it into the administration of those products.

    That being said, I don’t use PowerShell (PoSH) a lot. Scripting tasks is good for repetitive actions. However since I work at home and manage a couple instances, with disparate requirements, the time to write a PoSH script seems to outweigh the time it takes just performing a few tasks.

    I’m always looking for places that I can use PowerShell, and where it makes sense for me and I found one last year.

    Binary Data in SQL Server

    I wrote a presentation last year that looked at binary (BLOB) data in SQL Server, and worked with the FileStream/Filetable features. In building a demo, I wanted to show that binary documents in SQL Server work well with Filestream and that you can query the binary fields just like any other. However to render something like an image, you really need to stream the data into an application, or a file.

    I chose a file, and built a short PoSH script to do just that. Here’s the script:

    .csharpcode, .csharpcode pre
    {
    font-size: small;
    color: black;
    font-family: consolas, “Courier New”, courier, monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
    }
    .csharpcode pre { margin: 0em; }
    .csharpcode .rem { color: #008000; }
    .csharpcode .kwrd { color: #0000ff; }
    .csharpcode .str { color: #006080; }
    .csharpcode .op { color: #0000c0; }
    .csharpcode .preproc { color: #cc6633; }
    .csharpcode .asp { background-color: #ffff00; }
    .csharpcode .html { color: #800000; }
    .csharpcode .attr { color: #ff0000; }
    .csharpcode .alt
    {
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
    }
    .csharpcode .lnum { color: #606060; }

    $server = "SevenFalls"
    $database = "AdventureWorks2008"
    $query = "SELECT TOP 10 Document, FileName +'.' +  FileExtension FROM Production.Document WHERE Document IS NOT NULL"
    $dirPath = "C:\Users\Steve\Documents\Presentations\UnstructuredData\Docs\"
     
    $connection=new-object System.Data.SqlClient.SQLConnection
    $connection.ConnectionString="Server={0};Database={1};Integrated Security=True" -f $server,$database
    $command=new-object system.Data.SqlClient.SqlCommand($query,$connection)
    $command.CommandTimeout=120
    $connection.Open()
    $reader = $command.ExecuteReader()
    while ($reader.Read())
    {
        $sqlBytes = $reader.GetSqlBytes(0)
        $filepath = "$dirPath{0}" -f $reader.GetValue(1)
        $buffer = new-object byte[] -ArgumentList $reader.GetBytes(0,0,$null,0,$sqlBytes.Length)
        $reader.GetBytes(0,0,$buffer,0,$buffer.Length)
        $fs = new-object System.IO.FileStream($filePath,[System.IO.FileMode]'Create',[System.IO.FileAccess]'Write')
        $fs.Write($buffer, 0, $buffer.Length)
        $fs.Close()
    }
    $reader.Close()
    $connection.Close()

     

    The script works by essentially setting variables to a server, database, and a query. In this case I use the AdventureWorks database from 2008 that includes FileStream data. I then open a connection to the instance, run the command and look through the results, streaming out the data in the “Document” field to a file which has the name from the FileName+FileExtension fields.

    If you run this on your system, assuming fix the file paths, you’ll get 9 new documents in the file system, each one a document from the AdventureWorks sample database.

    That’s the best example of where I’ve used PowerShell in my daily work, but I know if I had to manage multiple instances, I’d be using this tool all the time.

  • SQL Saturday #183 Albuquerque – Wrap Up

    This past weekend was the first SQL Saturday in New Mexico. I had written about our trip down last week, with a few fellow Coloradan’s that were speaking at the event. We drove to Taos on Thursday and stayed the night. Friday we skiied, and had a great time. The weather was great, and the snow was nice.

    Photo Feb 08, 9 59 58 AM

    Despite a mix of abilities and technologies, we had a fun riding the slopes and chatting on the lift rides up.

    Photo Feb 08, 10 42 12 AMPhoto Feb 08, 10 42 14 AM

    We even got a few tricks from Reeves Smith and Jason Horner.

    Jason Horner

     

    Reeves Smith

    Five of us skiied, with one non-skier diligently reviewing his presentation in the lodge. At the end of the day, we lined up for a picture before starting our drive.

    Photo Feb 08, 6 44 51 PM

    Friday night we drove down to Albuquerque, arriving around 5:30 or so. After a quick stop at our hotel, we headed over to the speaker dinner. It was held at a very popular local restaurant, and we had a large table reserved.

    ‘There were a lot of speakers, including quite a few from Utah and Colorado. By my count there were 3 people from UT and 11 from Colorado. On one hand I’m thrilled that neighboring states were coming out to support the event. On the other, I wish more New Mexico speakers were featured.

    Photo Feb 08, 7 25 13 PM

    We left a little early and headed back to the hotel. I had work to do on Database Weekly and a few others riding with me wanted to review presentations. As much as speakers try to be prepared and ready for events, many of us still need to review and double check our slides and code the night before. Always glad to see people taking time to ensure they will deliver a good talk to attendees.

    We arrived early on Saturday morning, easily finding the event with plenty of signage out front. This SQL Saturday was being held at a New Horizons training center, with classrooms in use for the talks. There were 5 or 6 vendors, set up in the hallways and it was good to see people supporting the vendors and talking with them.

    I went to one of the first sessions, Todd Kleinhans’ talk on building a SQL Server 2012 Lab. I was slightly late, and when I walked in, Todd was showing the finished lab. He had in his VMWare Workstation program

    • A Windows Server 2012 Golden Master VM
    • A Windows Server 2012 Core Golden Master VM
    • A Domain Controller VM
    • 4 SQL Server VMs

    As I sat there, Todd deleted all the VMs and then proceeded to delete the virtual disks from the file system. I was impressed that he was ready to do this and start out with nothing in a 75 minute session. Especially when we were 15 minutes in.

    From there Todd proceeded to install Windows Server 2012 twice (one GUI, one Core) and set them up, snapshotting them installs 5 times for the other VMs. Over the next 30 minutes he rebuilt his lap, although he stopped short of installing SQL Server due to time.

    Photo Feb 09, 8 20 59 AM

    It was an impressive presentation and one I think many of you would benefit from. I hope he gets the chance to deliver this at the PASS Summit and a few other places this year.

    The event ran very smoothly. There was always coffee, water, soda, and some snack in the small kitchen. Lunch was boxed, and there was plenty of food in there. I had some great chats with various people in the hallways throughout the day. I had the chance to catch up with Sri Sridharan and chat about events and some strategy for the future. It was good to laugh and joke with some of the speakers through out the day, and I even sat in on another session in the afternoon, learning more about columnstore indexes from Carlos Bossy.

    The end of the day had everyone gathered in one large room for lots of thanks, applause, and prizes. I got a panorama shot of the closing below.

    Photo Feb 09, 5 21 42 PM

    The people in Albuquerque were very nice and a number of them came to the after party. While some of the Saturday night gatherings are mainly speakers and organizers, this had a good group of attendees that had the chance to network with speakers and each other.

    Saturday night had me back in Taos and Sunday was an easy drive home.

    Overall this was a very well run event, and very smooth. It’s a testament to the hard work and preparation of the Albuquerque SQL Server User Group, and also the hard work by Karla Landrum of PASS. People had a good day, they learned a few thing, and the people I asked about the event told me they thought it was a great day of training and networking.

    I really enjoyed the event in Albuquerque, and I’m hoping there is another one in the future I can attend.

  • Database Maintenance Essentials – Oracle Training Days

    Presented at Oracle Training Days, 2013

    Abstract: SQL Server requires a minimal level of maintenance to ensure that the platform functions at an optimum level without any unnecessary outages or problems. This talk will look at the basics of the maintenance operations that should be performed on SQL Server on a regular basis. The topics covered include:

    • Managing MDF and LDF Files
    • Managing Indexes
    • Maintaining Statistics
    • Checking for Corruption
    • Creating Backups That Will Restore
    • Managing Maintenance Jobs

    I cover the reasons why maintenance is required and include a number of references to help you enable this maintenance in your environment.
    In some presentations I will also cover the Red Gate tools that make this maintenance easier to perform. This is optional.

    Slides: Download the PPTX

    Level: 100

    Length: 60 minutes

  • I’m Speaking at ….

    Last week I noted that I was speaking at the new SQL Intersection event. Hopefully I’ll see some of you there and get the chance to shake some hands.

    As I was writing that post, I included a “I’ll be there” graphic, which I’ve included here as well. It was the first time I’d been sent a graphic from an event that specifically mentioned my as a speaker. It’s a neat idea, not only for promotions, but also as a way for me to include a small memory of my blog of the event. Somehow it seems slightly more personal than the event logo.

    A few days after I received that graphic, I got another email, this one from Karla Landrum of PASS. She noted that she was sending out “I’m speaking at” graphics for the SQL Saturday events. I got mine yesterday for the Albuqerque event this weekend.

    sqlsat183_speaking

    I think this is a great idea, and I’ve updated my speaking schedule with the graphic. I think it brightens up the page, and I’m looking forward to the other logos that will come from future events.

    Promoting events is hard, and every interesting thing you can do will help. Banner ads are often blocked or ignored by many people, but I do think the logos and pictures embedded into posts like this one can help raise awareness.

    And they’re a nice keepsake for your online blog, whether you’re a speaker or a trainer.

    Thanks to Jonathan Allen for suggesting this enhancement. If you’ve got an idea to help grow the SQL Saturdays or other events, please let the organizers know. Growing the SQL Server community and exciting others to join is something we all can do.