Scripting DB Objects in Powershell – video transcript

I like our videos, and I think they're a great way to teach, and an easy way to demo what we're trying to do. It's a lot easier and quicker than explaining things in text. BUT, they're not so great for reference after the first viewing. Sometimes I just want to FIND the CODE I NEED…

In that vein, I occasionally make the effort to partly transcribe an especially awesome video. Today, we have Sean's "Scripting DB Objects in PowerShell". Remember, this is a rough, incomplete transcription. More of a reference, which I hope will be really useful.

It's hard to get people jazzed about new technologies; PS is just another scripting language, so who cares?  But this one is a lot easier to use, and it's got a lot to offer. (This was filmed while PS was in version 1.0).  Today we'll talk about SQL PS and scripting databases and DB objects.

To open SQL Powershell, go to SQL Server Management Studio, right click on any object in the Object Explorer, and select Start PowerShell. You'll get a command line window that's context sensitive [2:50] – it starts in the tree position where you clicked: i.e., if you right-clicked on your server, the path may look something like this:

PS SQLSERVER:\SQL\SERVERNAME\DEFAULT>

If you right-clicked on a table, it might look like this:

PS SQLSERVER:\SQL\MySERVERNAME\DEFAULT\Databases\MyDBNAME>\Tables\dbo.MyTABLENAME>

We talked about navigating Powershell in the Powershell basics course. I like being able to traverse these things like i'm traversing a directory structure. 

  • "Dir" will get you a list of all "folders" (objects) in that directory that you can access, 
  • "cd" allows you to switch directories (object levels),
  • "gci" is an alias for "get child item" which gets a listing of objects in the directory (like dir does).

Example: If we're in the path ….Databases\MyDATABASE\Tables>, we can type gci | format-table, Name, Status to get a list of table names and statuses formatted as a table.  [4:50] Use the -autosize flag to make the output more readable.

[6:10] To connnect to another box, you just have to change directory (cd). So from  PS SQLSERVER:\sql\, do a cd OtherServerName\Default to connect to the default instance, or cd OtherServerName, where you can get a list of instances on that server (using dir).

[10:35] – Scripting objects. This is the cool stuff. There are all types of reasons to script out database objects. For example, we can make this part of our change control process…doing this by hand is kind of a pain. Being able to do it this way is great, because you can just call it from the command line.

Script DB objects: gci | % {$_.script()}  Explanation: We're in the Tables node, so all child items (all tables) get piped (|) to a percent (%), which is an alias for FOR EACH. Variables start with $ in powershell, so $_ is the built in variable that holds the current cursor value. Reading the script line left to right, it says: get each child item (gci), pipe (|) it to a for each (%), which takes the current name ($_), and call the script method
(.script()).

[14:25] Basic filtering: gci *xyz* | % {$_.script()}  will only get those with xyz in the name. You can also pipe them to the filter object and filter based on very rich criteria.

[~15:00] Discussion of all the different methods you can call – not just .Script().  You can find out all the available methods with  gci | gm  (get-childItem, piped to get-member).

[18:15] Pipe your object scripts to an out file: gci | % {$_.script()} | out-file c:\scripts\MyScript.txt

Happy days,

Jen McCown

http://www.MidnightDBA.com
 

Demo: What’s the difference between CHAR and VARCHAR?

Content level: Beginner / refresher

At last week’s SQL Saturday 41 Atlanta, I gave my TSQL Beginner’s Kit session in the last time slot of the day, to deafening applause and accolades. Actually, there were about 12 of us in the room, and we had a really good time.  One of the more popular items in the session was a simple demonstration, illustrating the key difference between the SQL Server datatypes CHAR and VARCHAR.  To review:

  • CHAR is a fixed length string data type, so any remaining space in the field is padded with blanks. CHAR takes up 1 byte per character. So, a CHAR(100) field (or variable) takes up 100 bytes on disk, regardless of the string it holds.
  • VARCHAR is a variable length string data type, so it holds only the characters you assign to it. VARCHAR takes up 1 byte per character, + 2 bytes to hold length information.  For example, if you set a VARCHAR(100) datatype = ‘Jen’, then it would take up 3 bytes (for J, E, and N) plus 2 bytes, or 5 bytes in all.

You can see how the use of VARCHAR in most cases is preferred, to save space.  The demo I used to illustrate this was a very simple script:

DECLARE @myChar CHAR(100),
 
@myVarchar VARCHAR(100)
SET @myChar =‘Jen’
SET @myVarchar =‘Jen’
select‘[BEGIN]’+ @myChar +‘[END]’as Char_Data
select‘[BEGIN]’+ @myVarchar +‘[END]’as Varchar_Data 

Here is the result when you run the script:

Char_Data
[BEGIN]Jen                                                                                                 [END]
 
Varchar_Data
[BEGIN]Jen[END]

See? It’s just a nice visual of all the extra padding (and space wasted) CHAR uses.  The next tim someone asks you, “Hey, what’s the big deal? So I have 400 tables with CHAR(3000), so what?” you can point them right here.

Happy days,
Jen McCown
http://www.MidnightDBA.com

 

Informal Poll: Suspicious Job Offer?

A friend wrote me (and a few others) today to get opinions. Edited for content/anonymity:

I interviewed with one person (head of their office) who admitted he knows nothing about databases.  He asked me a few general questions, told me that my resume was really impressive and I seem to be very knowledgeable. I asked my questions, and that was it.  I was out of there in about 45 minutes.

There was no second round interview, no tech interview.  This morning they called and offered me the job.  This seems rather shady…they never asked me a single technical question!

I'm flattered to have been offered this job, but I'm really not sure what to do here.
This place seems to have the things I want – clustering, HA, etc – but I really don't know much else about them.

Based on your experiences, does this seem strange to any of you? Would I be out of order to ask for a second interview so I can meet more people and?

I asked Twitter: What would you think about a company that offers you a job without a tech interview of any kind? Suspicious? Normal?  Here are the replies so far:

  • probably depends on the company and its culture. Was the rest of the interview process rigorous?
  • I'd guess a small company where there's no one qualified to do the tech interview. Or it's a sucky enviro & they're desperate.
  • I would be very cautious. I had contracting firm do that who had yet to win contract on AF base.
  • I'd find that strange but like @mikeSQL said they might already know your skill level since you are famous
  • no tech interview?Sometimes smaller companies w/o a SQL guy will take resumes at face value. Don't know enough SQL to vet.
  • That means that you didn't get the chance to interview them to see how they would fit with you, run away as fast as you can.
  • I would b a lil susp.
  • perhaps theyre already familiar with your #awesomesauce skillz?
  • depends entirely on whether they know you and your work already, but usually suspicious
  • depends. did the do a background check including your resume? if so & they respect where you worked (e.g. MSFT), might be legit
  • I'd be very afraid. To me, that signals that they don't care about their business/company and will just take anyone.

That's a very wide range of responses…I guess the best bet is to go with your gut…and request the second interview…

-Jen "just reporting the news" McCown

http://www.MidnightDBA.com