Category: Blog

  • Finding #dbatools #PowerShell commands

    I really like the dbatools project. This is a series of PowerShell cmdlets that are built by the community and incredibly useful for migrations between SQL Servers, but also for various administrative actions. I have a short series on these items.

    I was reading about Pester from Rob Sewell and saw he talked about a new command I hadn’t seen in dbatools: Find-DBACommand. This is a fantastic idea, and I think it’s my new favorite dbatools command, err, cmdlet.

    This is the search cmdlet for dbatools. With this, I can pass in a parameter of something I’m looking at and get results. For example, if I run this with “attach”, I get:

    2017-11-16 08_58_34-cmd - powershell (Admin)

    If I use “security” I get:

    2017-11-16 09_00_15-cmd - powershell (Admin)

    These are the ways that I’d most often use the command, but I can certainly do other things. There are tags on some cmdlets, so I can do this: Find-DbaCommand –Tag job

    2017-11-16 09_03_31-cmd - powershell (Admin)

    This gets me less results than just a search for the string “job”, and these are items focused on jobs themselves.

    I can loo for specific authors with the –Author parameter. I can also look for minimum or maximum versions of SQL Server, though I haven’t found anything that returns here in quick testing. In looking through some help and source code for other commands, I don’t see version tags, but hopefully that comes over time (Submit your PRs here).

    This is a fantastic addition, and certainly better than trying to search all of PoSh help for something like Backup. I might get way more commands I don’t care about there.

    If you haven’t tried dbatools, do it today. It’s a fantastic administration tool for your toolbelt.

  • One More Trip and Down for the Count

    The last month has been a rough one for me, with lots of travel. I was on trips for 4 out of the last 5 weeks, and the week I was home I had a remote presentation to do. Travel can be hard enough, but add in the need to continue to manage the day job as well as prepare and practice for presentations, and it’s a long stretch.

    Fortunately, I have a month off. Or nearly so. I have one last trip in December, and then I’m done for the foreseeable future. I have nothing scheduled in Q1 for travel, and I’d like to keep it that way. Apart from trip to visit the Redgate offices and a few customers, I’ll likely take quite a bit of 208 off from speaking at conferences. I’ve done a lot the last couple years and I need a break.

    Plus there are other projects with Redgate and SQLServerCentral I’d like to tackle, not the least of which is upgrading our infrastructure from SQL 2008/WS2003.

    I’ll still aim to get to a few new events next year, and I do appreciate the invitations. You’re always welcome to ask, but understand I’ll do less. I’d like to visit some new places, and Pittsburgh is tops on my list for now.

    It’s been a pleasure seeing and meeting so many of you the last few years. I hope you’ve enjoyed my talks, and I’ll certainly continue to do more in the future, just at a slower pace.

  • Keeping the Single User Connection

    A reader was having issues with a script. They ran their code from PoSh (Invoke-Sqlcmd), setting single user and then trying to rename a database and its files. However, occasionally, they’d lose the single user connection to another user. Frustrating, but it wasn’t a PoSh issue.

    The ALTER DATABASE commands don’t really specify how SINGLE_USER works. What happens is that if you set single user, the first user gets the connection, whether that’s sysadmin, dbo, or a regular user. This means that when you set a database to single user mode, you need to be one of the users in the database.

    The change to single used is blocked, until all other connections disconnect. One way to ensure this happens quickly is to add the WITH ROLLBACK IMMEDIATE clause, which disconnects users.

    The original poster’s issue was that while they used the clause, their connection was in the master database. This meant that another user could potentially connect to the database, grab a shared lock, and prevent renaming files.

    If your connection is in the user database, then when you issue the ALTER DATABASE WITH ROLLBACK IMMEDIATE and disconnect users, then you have the only connection. This should prevent any issues.

    Quick Demo

    Let’s see this in a simple way. I’ve got a demo database that I use, and I’ll open three connections. The first will be as a sysadmin here:

    2017-10-24 09_54_17-Connect to Database Engine

    and then the other windows I’ll switch the connection to a non-privileged user.

    2017-10-24 09_55_17-SQLQuery20.sql - Microsoft SQL Server Management Studio

    At this point, I have a few connections to my database.

    2017-10-24 10_00_48-SQLQuery19.sql - (local)_SQL2016.EncryptionDemo (PLATO_Steve (56))_ - Microsoft

    Let me now reset the database option.

    2017-10-24 10_01_30-SQLQuery19.sql - (local)_SQL2016.EncryptionDemo (PLATO_Steve (56))_ - Microsoft

    Everything works here, let’s check the next session. If I switch to another tab, I’ll run a query. After a second or two, I get this:

    2017-10-24 10_02_29-SQLQuery21.sql - (local)_SQL2016.EncryptionDemo (AEUser (61)) Executing..._ - Mi

    SQL Server has disconnected my session. If I try to reconnect (this login has EncryptionDemo as the default), I get this:

    2017-10-24 10_17_04-SQLQuery22.sql - Microsoft SQL Server Management Studio

    If you need to perform maintenance, and want to ensure you have the connection, change context to the database before setting single user mode.

  • Adding a Format SQL Button to the Redgate Toolbar

    I’ve gotten used to CTRL+K,Y to format SQL with SQL Prompt, but a customer wanted a button on the toolbar. It’s fairly easy to do, but I thought I should document the process for others.

    First, if you click the small area on the right of a toolbar in SSMS, you’ll get an “Add or Remove Buttons” menu, as shown here.

    2017-11-14 16_29_31-~vs7FB.sql - DKRSPECTRE_SQL2016.sandbox (DKRSPECTRE_way0u (52))_ - Microsoft SQL

    If you click that, you’ll see this dialog.

    2017-11-14 16_29_39-~vs7FB.sql - DKRSPECTRE_SQL2016.sandbox (DKRSPECTRE_way0u (52))_ - Microsoft SQL

    In  this case, I’m happy with the buttons, I want to customize my toolbar. Click that option. This opens up the dialog below, and I’ll want to click the “add Command” to add a menu item as a button.

    2017-11-14 16_29_50-Customize

    From here, I get a list of all SSMS menus. In this case, I’ve clicked the Dimension item, and I can see all the possible menu items on the right.

    2017-11-14 16_30_02-Add Command

    However, I want a SQL Prompt item, so I need to scroll down the left to SQL Prompt. Once I click that, I see the commands on the right,

    2017-11-14 16_30_11-Add Command

    Now scroll the right to find Format SQL.

    2017-11-14 16_30_23-Add Command

    Click OK and then your new button appears in the list.

    2017-11-14 16_30_32-Customize

    And on the toolbar.

    2017-11-14 16_30_41-~vs7FB.sql - DKRSPECTRE_SQL2016.sandbox (DKRSPECTRE_way0u (52))_ - Microsoft SQL

    Now you GUI clickers can reformat SQL quickly.