MidnightDBA and Petri.co.il – US US US ME ME ME!!

Sean and I have been quietly writing SQL content for the release of the Petri IT Knowledgebase’s new SQL site, which has happened just this week!  Not only that, but Petri is publishing a press release tomorrow which will heavily feature us, your MidnightDBAs!!  This is really exciting stuff for us, and there’s even more super secret ninja #awesomesauce in the days to come!

Hey, as long as we’re blowing our own horn, I should mention the oh-so-VERY-nice blog post that Brad McGehee wrote about Sean’s NTSSUG class.  For the last several months, Sean’s been teaching a progressive SSIS class, called Ground Zero SSIS for People who Want to Learn SSIS and Want to Learn to do Other Stuff Good too, before the regular North Texas SQL Server User Group meetings.  We talk all the time about how difficult it is to really learn a subject in an hour-long session; this series that Sean’s giving through the year (and posting free on our site!) is meant to counter that limitation. 

The North Texas User Group meeting attendance has been growing like gangbusters – our UG president recently attributed our more than 100% growth this year to Sean’s sessions.  I’m proud as hell of my hubby, and I’m really delighted that other people are taking note of all that he’s giving back to the SQL community. 

So.  Um, where was I?  Oh yeah, Brad! You should all totally check out Brad’s material on his blog and on Twitter.  And, of course, my MidnightDBA blogcast #17 featuring his blog Common Dev/DBA mistakes. 🙂

Okay, enough about us. Hey, I think I’ll finish my blog post about how WE saved SQL Saturday #35! 🙂 In the meantime, here is more US to tide you over.

Happy days,

Jen McCown

http://www.MidnightDBA.com

Script SQL Objects with Powershell

Like so many of you out there in Internetland, I’m fairly new to Powershell, which is fairly new to the world. Recently I wanted a simple way to script out database objects, and a couple of you were kind enough to share your solutions with me.

Sean Likes Powershell

I also worked with Sean, who likes very much to talk about Powershell (I recommend you start with What Makes Powershell Awesome?, my personal favorite blog on PS). Here is the current list of his Powershell videos:

Yes, I am bragging on him. You should listen to your friend Sean, he’s a cool dude.

I Like Powershell

Anyway, the Scripting DB Objects in PowerShell video was almost what I needed, but not quite. Sean goes over how to script out objects to a single file; I’m scripting objects to check into source control, so I want one object per file. Last night we finally got around to sitting down so he could show me the code.

dir | %{$Table = $_.Name; $_.script() | out-file c:\$Table.txt}

That’s it. That’s everything you need to script out every single table in a database to an individual file. I <3 Sean, and I <3 Powershell. At the risk of stealing his thunder, let me `splain a little.

We All Like Powershell!

There are SMO solutions, but there’s a definite efficiency, elegance, and convenience to having everything you need in a single line of code.

dir | %{$Table = $_.Name; $_.script() | out-file c:\$Table.txt}

Start SQLPS from within SSMS 2008 (just right-click your database of choice in the Object Explorer, and “Start Powershell”). The SQL Server Powershell will open, with the path to your chosen database already loaded. Type cd Tables and hit Enter to navigate to the Tables “folder”. Now, to break down the Powershell command:

  1. dir
    dir
    gets a listing of all the tables in that database
  2. dir |
    the pipe (|) pipes that list to the next part of the command
  3. dir | %{}
    %{}
    is the shorthand version of a FOR loop in PS*. So this FOR loop is FOR each table piped from dir
  4. dir | %{$Table = $_.Name}
    $Table
    is a variable. We set it = the current table name passed in to the loop, which is signified by $_.Name. Note that $_ always means “the current one”, in this case “the current table”.
  5. dir | %{$Table = $_.Name; $_.script()}
    Within the loop, after we set $Table = the current table name, we make the command to script out that current ($_) table with $_.script()
  6. dir | %{$Table = $_.Name; $_.script() | out-file c:\$Table.txt}
    We pipe the output of that scripting function (|) to an out-file command, with the path that we want to use. If we wanted to, we could use a single file andu use -append; but I want one file per table, so we use a filepath plus $Table.txt (here, c:\$Table.txt)

That’s all there is to it. It seems like gibberish at first, but that’s why they call it “code”, right?

Let me take a moment here to throw a little love in Allen White’s direction (blog, @sqlrunr on Twitter). Allen also likes to talk about Powershell and SQL, and is also a really cool dude, so he’s yet another resource to check out on the path to enlightenment.

Happy days,

Jen McCown

http://www.MidnightDBA.com

* Sean says, “Don’t forget your curlies!”

Converting MS Office Files

This is a very quick note on converting Office files, because I know I’ll need it someday…

Our dear friend @LadyRuna (Andie LeTourneau) asked for help on Twitter today; someone had sent her an Excel file that was saved as a later version than she had installed. After many recommendations from the Tweeps, she said:

High Five from ME to OpenOffice.org – Opened xlsx in Calc, converted to csv. Now import to SQL time. Thanks to all for suggesitons #sqlhelp

That will DEFINITELY come in handy some day, I just know it.

News

By the way, we got all moved in to the new house yesterday; it was a VERY long day, but we’re still very excited.

Sean presents at NTSSUG tonight at 6pm, followed by Brad McGehee (blog, Twitter) at 7ish. Tomorrow is SQL Saturday35 Eve, and Saturday is the big day!

-Jen

http://www.midnightdba.com