Author: way0utwest

  • Changing the Owner of a Database #SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    I had an authorization issue with my account, and I decided to delete it and re-add it. That’s the subject for another day, but before I could delete it, I had to remove the ownership of some databases. You can’t delete a login that owns databases.

    I realized I wasn’t sure how to do this, so I wrote this post.

    A Deprecated Proc

    There used to be a dbo.sp_changedbowner proc that was used, but I know this is deprecated and it shouldn’t be used. It likely would work fine in SQL Server 2019, but I also know there should be more modern code. I decided to look, as I ought to know what is recommended these days.

    In searching around MS Docs, ALTER AUTHORIZATION comes up in the list. I checked, and this allows me to transfer the ownership of a securable, which a database is one of the items in the list. Example F shows what I want to do and uses this code:

    ALTER AUTHORIZATION ON DATABASE::dbname TO [login]

    I can replace dbname and login with the values I need.

    Which Databases?

    I have a lot of databases, and I don’t need to change them all, though I could. In my case, I decided to get a list of databases and owners. If you query sys.databases, there is an owner_sid column. If you join that with sys.server_principals, you can do so on the SID column. This query shows me what I need:

    SELECT d.[name], sp.[name] FROM sys.databases d
      INNER JOIN sys.server_principals AS sp
       ON d.owner_sid = sp.sid

    The results are here:

    2022-02-25 12_34_10-SQLQuery1.sql - ARISTOTLE_SQL2017.master (sa (54))_ - Microsoft SQL Server Manag

    In some sense I hate that “sa” isn’t the default owner, but I get it. There might be a need for other accounts. However, my account is a sysadmin, so my view here is that “sa” ought to be listed.

    I digress. Now that I have a list, I can limit it to my account with a WHERE clause. I can take that list of items and build the code. I could use a cursor, but this is a one-off task, so this works:

    SELECT
                    'ALTER AUTHORIZATION ON database::' + d.[name] + ' TO sa;'
                  , d.[name]
                  , sp.[name]
    FROM
                    sys.databases d
         INNER JOIN sys.server_principals AS sp
             ON d.owner_sid = sp.sid
    WHERE          sp.name = 'ARISTOTLE\Steve';
    GO

    This gives me the code in the results I want to run. I copy paste this and I have a bunch of statements to run. 

    2022-02-25 12_41_07-SQLQuery1.sql - ARISTOTLE_SQL2017.master (sa (54))_ - Microsoft SQL Server Manag

    Despite Grammarly not being happy, this worked fine.

    SQL New Blogger

    As soon as I realized I needed to do this, I knew there were two posts here. One on the removal and adding back of my Windows account, and the second on this topic (when the first didn’t work).

    This took about 15 minutes extra, finding the docs and writing some code, but it’s a good example of where a small situation that occurred helped me find something to write about. Easy for you to take little tasks like this and document your knowledge when you learn something.

  • Daily Coping 14 Mar 2022

    I started to add a daily coping tip to the SQLServerCentral newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag.

    Today’s tip is to take three calming breaths at regular intervals during the day.

    This is an easy one for me. Since I’ve been practicing yoga for 10-12 years, I often find myself taking more conscious breaths at times. Fill up deeply, hold briefly, and slowly let them out.

    It’s amazing how calming 3-4 deep breaths can be.

  • A Frozen UI at 65MPH

    This is part of a series that covers my experience with a Tesla Model Y.

    I was driving home from a ski trip recently on I-70 E in Colorado. My wife wanted to call my daughter and went to switch the connected Bluetooth device from my phone to hers. She connected her phone and made the call, but she couldn’t get the audio to come out of the car speakers. She ended up talking directly on the phone while trying to disconnect and reconnect a few times.

    At some point my wife finished her call and went to click the icon on the Tesla screen to disconnect and nothing worked. She tried pressing the car menu button, the spotify icon, even the climate controls. Nothing worked.

    At this point I was driving 65mph on a busy highway, with snow on the side shoulders. Not the place to worry.

    I knew the car should keep running, but there is something a little uneasy about not being able to change any settings while driving down the road. We debated what to do for a few minutes. I knew there was a reset procedure, but I wasn’t interested in testing it while driving.

    We kept going down the highway, planning on taking an exit and then rebooting. As my wife searched for information online as to what to do, the screen actually restarted itself. Not the full restart I’ve seen with software updates, but apparently a soft restart of the UI. After 10-15s after the screen went dark, I had a working UI again.

    Hard and Soft Reboots

    We looked up the procedure and there are both hard and soft reboots of the car. A soft reboot can be done in the Model Y by holding both steering wheel buttons down until the car resets. This should only be done while the car is stopped, which is what I expect.

    A hard reset, and power cycling the car, comes from the UI itself. You should allow the car to be powered down for 2+ minutes before doing anything.

    In general, I’m not overly worried about the UI rebooting, but I do think that this is a case where a secondary screen, running an independent OS, would be good to keep the driver informed of at least speed.

    Am I Concerned?

    Not now. The car seems to work independently of the UI, which is what I would hope was the case. It’s lightly disturbing, but that’s about it. Now I know what to expect.

    Software Updates

    Usually there’s a notification in my app of a software update. It also appears in the UI. I used the UI to do one manually, watching the screen give status info similar to how a PC or mobile does. Very uninteresting.

    For all of them since the first one, I usually just approve the update in the app at night. The car is plugged in and I haven’t worried about watching it. I’ll check in the am, and I haven’t had issues.

    I do see UI elements moved, which is slightly annoying, but not critical. Mostly I watch the release notes would better use images to describe movement of stuff so I don’t have to hunt around. It took me 3 or 4 minutes to find my steering wheel heater after one update, which was annoying and distracting as I kept looking at various stop signs during a drive.

  • Republish: IoT Pros and Cons

    I’m at SQL Bits today, the last day of the conference. I need to write an update to this one, but you can re-read IoT Pros and Cons for now.