Category: Blog

  • SSoL–Elevating Privileges

    I don’t think most of us need to know Linux, but if you end up managing a system, it’s good to have a little idea of how to get around. This is a short series of posts as I remember the skills I used to have back in university.

    One of the things that you might find the need to do on a Linux system is elevate your privileges. By default when we connect and work on Linux, we are working as normal users. We do this in Windows, but when we need more privileges, Windows will give us a UAC prompt. Linux doesn’t.

    For example, when I want to check for updates of software, I use apt-get. That doesn’t work for my normal user:

    2017-06-15 09_54_59-Ubuntu 64-bit SQL Server .210 - VMware Workstation

    Instead I need to use sudo to elevate privileges. You prefix a command with sudo, enter the root password, and you get elevated privileges, as shown here.

    2017-06-15 09_57_14-Ubuntu 64-bit SQL Server .210 - VMware Workstation

    If you just enter sudo -i, then you get the shell to switch and all commands execute as root.

    2017-06-15 10_00_09-Ubuntu 64-bit SQL Server .210 - VMware Workstation

    Don’t do this. DON’T. Work as a normal user until you need higher privileges. For the most part you don’t.

  • Adding Performance Counters back for SQL Server

    I had a strange situation the other day, where a number of things went wrong with my instance. First, I lost permissions to detached databases. The SID was listed in the file permissions, but apparently unlinked to an account.

    Next, I went to add an alert, and I only had the XTP counters.

    2017-06-14 12_16_20-SQLQuery4.sql - (local)_SQL2016.sandbox2 (PLATO_Steve (63))_ - Microsoft SQL Ser

    The counters are also missing in Performance Monitor. What is interesting is that I show the correct SQLAgent counters for each of my three instances.

    2017-06-16 08_54_19-Add Counters

    A quick search found me this blog on MSDN, where it recommends the following:

    unlodctr mssqlserver
    
    lodctr perf-mssqlserversqlctr.ini

    I had a named instance, so for me I entered:

    unlodctr mssql$sql2016
    
    lodctr perfMSSQL$SQL2016sqlctr.ini

    from an elevated command prompt. Running the last command again shows the counters loaded.

    2017-06-16 09_19_26-cmd (Admin)

    I also checked my registry, which appeared to be fine:

    2017-06-16 09_08_38-Adding Performance Counters back for SQL Server - Open Live Writer

    I next found another blog that noted I might need to resynch WMI, so I ran winmgmt, using the PID from Task Explorer (details tab):

    2017-06-16 09_24_32-cmd (Admin)

    I didn’t see counters at first, but I restarted the instance. Once that was done …

    2017-06-16 09_23_29-New Alert

    A nice fix, and one I probably won’t forget after this blog.

  • Loading All CSV Files with PowerShell

    I ran across Denis Gobo’s post about working with names from Social Security cards and wanted to play with the dataset. However, rather than use xp_cmdshell, which I might have, I decided to use PowerShell. I need to repeat this with Python, but for now, this worked.

    There is a file you can download that has a lot of .txt files, each one with a CSV. You can read about this in more detail in Denis’ post. Essentially there were three fields, and the year of the file in the name. I downloaded and unzipped the files into a folder.

    Here was my process:

    1. Loop through the files
    2. Extract the year from the file name
    3. bcp in the file into a staging table
    4. call a proc that takes the year as a parameter and moves all data

    With that in mind, here’s how I set things up.

    The SQL Server Side

    I started by creating two tables.

    CREATE TABLE Names
    (   FirstName  VARCHAR(500),
         Gender     CHAR(1),
         NameCount  INT,
         YearInFile INT
    );
    CREATE TABLE Names2
    (   FirstName VARCHAR(500),
         Gender    CHAR(1),
         NameCount INT
    );

    With these in place, I created a procedure:

    CREATE PROCEDURE MoveNames @year INT
    AS
    INSERT Names SELECT FirstName, Gender, NameCount, @year FROM Names2;
    TRUNCATE TABLE Names2;
    GO

    I tested these and they worked fine for my process. I want to load into Names2, then to get the year, I move the data to the other table, adding the year. There are probably better ways, but this worked fine for a relatively small load (few million rows).

    PowerShell

    With PoSh, I started with a simple loop. I tend to set variables first since this makes this easier to turn into a function.

    $sourcefolder = “E:\Downloads\names”

    $sourcefiles = Get-ChildItem $sourcefolder -Filter “*.txt”

    foreach($file in $sourcefiles ){
       $yearinfile = $file.Name.Substring(3, 4)
       write-host “Loading File:” $file.Name ” year: ” $yearinfile

    }

    Running this gets me a list of all files along with the  years. That’s what I want, since I’m going to process each file and load it in with bcp. Thanks to Mike Fal for the outline of how I’ll do this.

    Next, I set more variables at the top:

    $InstanceName = “.\SQL2016”
    $DatabaseName = “Sandbox”
    $StagingTableName = “Names2”
    $StagingProc = “MoveNames”

    I’ll use these in this code. I call Invoke-Expression to run bcp and then Invoke-Sqlcmd to run my proc.

    $cmd = “bcp ‘$DatabaseName.dbo.[$StagingTableName]’ in ‘$file’ -S’$InstanceName’ -T -c -t’,’”

    Invoke-Expression $cmd
    Invoke-Sqlcmd -ServerInstance $InstanceName -Database $DatabaseName -Query “$StagingProc $yearinfile”

    From here, I put this in a file and ran it from my download location. The result:

    2017-06-16 12_41_08-Windows PowerShell ISE

    and from SQL Server:

    2017-06-16 12_41_50-SQLQuery7.sql - (local)_SQL2016.sandbox (PLATO_Steve (56))_ - Microsoft SQL Serv

    Now I can run some queries, and find out where I stand. #26, it turns out.

    2017-06-16 12_43_35-SQLQuery7.sql - (local)_SQL2016.sandbox (PLATO_Steve (56))_ - Microsoft SQL Serv

  • Move a Stored Procedure to a New Schema–SQLNewBlogger

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

    One of the things I needed to do recently was move an object. I was testing the WideWorldImporters database and created an object in the dbo schema. That’s the default for me, which is fine. However, in this case I wanted it in a different schema.

    The way to do this is with the ALTER SCHEMA command. There is a TRANSFER option, which takes the original schema and object name.

    In my case, I had the dbo.GetOpenPurchaseOrderCount procedure in my database.

    2017-06-07 14_40_29-SQLQuery1.sql - (local)_SQL2016.WideWorldImporters-RR (PLATO_Steve (53))_ - Micr

    I used this command to move it.

    ALTER SCHEMA Website
    TRANSFER dbo.GetOpenPurchaseOrderCount

    And then verified things moved.

    2017-06-07 14_40_37-SQLQuery1.sql - (local)_SQL2016.WideWorldImporters-RR (PLATO_Steve (53))_ - Micr

     

    SQLNewBlogger

    This was one of those quick items where I checked the ALTER commands, thinking it was in there. I didn’t see a changeobjectschema procedure, and since this was a new skill, it was a 5 minute blog.