Tag: SQLNewBlogger

  • Watch Your DataTypes in Aggregates–#SQLNewBlogger

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

    I’ve got a database of NBA statistics with data like this for players. I downloaded a CSV and loaded it into SQL Server.

    2017-03-22 10_32_00-SQLQuery1.sql - (local)_SQL2016.NBA (PLATO_Steve (102))_ - Microsoft SQL Server

    I decided to play with the data a bit and at one point wanted to see who scored the most points for a team and year. So I ran this query:

    SELECT
        year,
        team,
        MAX(pts)
    FROM dbo.player_regular_season
    WHERE
        year = ‘1972’
        AND team = ‘LAL’
    GROUP BY
        year,
        team;

    The result was 705. That’s a decent number of points, and if I weren’t careful, this might seem fine. 1972 was a long time ago, and they didn’t score as many points as they do today in games.

    In fact, if I were putting this in a summary report with lots of data, it might be the case that someone glancing at this would make a poor decision based on the data.

    Why?

    Let’s look at the data.

    2017-03-22 10_46_16-SQLQuery1.sql - (local)_SQL2016.NBA (PLATO_Steve (102))_ - Microsoft SQL Server

    Even a quick glance would let me know this seems funny. There are values of 1575 and 1084 in there, but the MAX() I returned was 705. If I look deeper at the import, I can see why.

    2017-03-22 10_47_25-SQLQuery1.sql - (local)_SQL2016.NBA (PLATO_Steve (102))_ - Microsoft SQL Server

    Anything stand out there? If you look, pts is a varchar, not a numerical value. In the character world, 705 beats 1575. I really need this query:

    2017-03-22 10_48_30-SQLQuery1.sql - (local)_SQL2016.NBA (PLATO_Steve (102))_ - Microsoft SQL Server

    Always be aware of the datatypes you work with and manipulate. Knowing a little bit about the meaning and use of the data can help you spot anomalies like this. As much as I like random test data, I’d also be sure you have some real data cases when you have users check your work. It’s easy for them to miss problems like this without good reference cases.

    Or use good test data that you’ve setup and unit tests.

  • Getting a VHD into Azure with PoSh

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

    I thought this would make a nice SQLNewBlogger post, an easy get started one. I used the docs from Microsoft as a guide, so here’s how it went.

    Login to my account. I used Add-AzureRMAccount here to get the login dialog. I’m not repeating this for now, so this is just an interactive test.

    2017-03-23 14_09_25-Sign in to your Microsoft account

    Now I tried to create a storage account, but I hadn’t assigned a subscription. I didn’t think I needed to  since I only have one,but hey, I guess I do.

    2017-03-23 14_11_36-cmd - powershell

    I started typing Select-AzureSubscription, but realized I didn’t know what it was. I can’t remember naming this and under my name in the portal, I didn’t see a place to find it.

    2017-03-23 14_13_22-Getting a VHD into Azure - Open Live Writer

    Ah, under All Resources I see something, so I’ll try this.

    2017-03-23 14_12_26-All resources - Microsoft Azure

    On second thought, under My Permissions, I see it there.

    2017-03-23 14_14_17-My permissions - Microsoft Azure

    Or not

    2017-03-23 14_14_41-cmd - powershell

    Get-AzureSubscription doesn’t quite work.

    2017-03-23 14_17_54-cmd - powershell

    It’s Get-AzureRMSubscription

    2017-03-23 14_19_02-cmd - powershell

    So, why doesn’t my creation of the storage account work? I should really read error messages. The issue is Select-AzureSubscription fails because I need Select-AzureRMSubscription.

    Sometimes the rev’ing of the cloud is hard.

    Actually, maybe neither work.

    2017-03-23 14_22_02-cmd - powershell

    I retreat.

    2017-03-23 14_23_59-cmd - powershell

    I’m still annoyed. “US West” isn’t valid. FFS, Microsoft. Whether I type any of these, just ask me or process them.

    • “US West”
    • “West US”
    • “USWest”
    • “US_West”

    OK, I decided to go exercise for a bit.

    I’m back (30 minutes later), and I try Get-AzureLocation. I can guess some cmdlets. I get a lot of stuff back and see “West US” is valid. I try a few other valid names of regions, but get errors. It’s not that parameter.

    Let’s try a new name.

    2017-03-23 14_30_22-cmd - powershell

    #$%#@#@# engineers and developers. That’s a valid error message? Not, this is a duplicate or something that makes sense. Glad I left.

    Maybe not. At least this is an error that makes sense.

    2017-03-23 14_31_27-cmd - powershell

    Finally. Let’s upload the VHD.

    2017-03-23 14_33_25-{0%} cmd - powershell

    And it’s off.

    2017-03-23 14_33_41-{0%} cmd - powershell

    Hopefully this will work. I haven’t tried this with PoSh before, and it was somewhat frustrating, though it really didn’t take that long.

    SQLNewBlogger

    This was a bit of a live blog. Do something, take a screenshot, write some text. It was a bit of a learning experience.

  • Delete an Azure SQL Database from PowerShell

    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’ve been working on is trying to get my DevOps, continuous delivery pipeline working with Azure. Part of that is a test deployment to an Azure SQL Database, which means I need to be able to update an existing database from a backup. Unfortunately, Azure SQL Database doesn’t support a restore over an existing database (yet).

    That means one task I have is to remove an existing database, in order to replace it with a new database with the same name. A little work in the PoSh documentation found Remove-AzureRMSQLDatabase, which is just what I need.

    To use this cmdlet, I need a connection to the Azure space first. I can do that with a credential that I get with this command. Ultimately I need to store this, but interactively this lets me get started:

    Login-AzureRMAccount

    This gets me an interactive login. I enter my account and password. Since I use a Live account, this won’t work in the pipeline, but it gets me going.

    2017-03-10 09_00_34-Sign in to your account

    From here, I can set a few variables I’ll need. I want the name of a resource group, a server, and a database. In my case, I’ll use a few variables. I call the database the “new” one, since I’ll be using an existing one that I’ll recreate from an “old” one.

    2017-03-10 09_03_07-powershell - How to Login without prompt_ - Stack Overflow

    From here, it’s just a question of calling the Remove-AzureRmSqlDatabase cmdlet with parameters. I do that, and get results. Here’s the call

    Remove-AzureRmSqlDatabase -ServerName $server -ResourceGroupName $rgname -DatabaseName $newname

    Here are the results. I’ve blacked out a few ids.

    2017-03-10 09_05_16-Photos

    This clears the database, and after refreshing, I can see it’s gone from my list of Azure SQL Databases.

    2017-03-10 09_08_12-SQL databases - Microsoft Azure

    Not much to this, but it’s part of a larger scheme, which is getting a copy of the production database and restoring it.

  • Create a BACPAC–#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 that you might encounter at some point is the need to create a BACPAC. This is essentially a DACPAC with data included. There are times you might find a BACPAC more convenient than a full backup, especially if you work with Azure.

    This post shows how you can create a BACPPAC file (with the extension .bacpac) from SSMS.

    I have a small database, the PartsUnlimited database, from the Microsoft PartsUnlimited demo application. I want to create a BACPAC from this, however, when I right click and select Tasks, there is no “Create BACPAC” option.

    2017-02-21 14_08_41-SQLQuery1.sql - dkranchlabdemo.database.windows.net.master (sjones (113)) - Micr

    That’s fine, because the “Data Tier Application” is the DAC, and an export includes the data, which creates the BAC part of the process. Let’s choose “Export Data-tier Application”. Once we do that, we get the expected summary wizard screen to start.

    2017-02-21 14_10_05-Export Data-tier Application 'PartsUnlimited'

    I click next and immediately get prompted for a location in which to save the file. The will include all your data, so choose a location that has sufficient disk space. You have the option to also store this in Azure if needed.

    2017-02-21 14_10_43-SQLQuery1.sql - dkranchlabdemo.database.windows.net.master (sjones (113)) - Micr

    Note the filename above is already filled out by default, but you can change this in the edit box. If you click the “Advanced” tab, you’ll get the chance to select only certain tables if that is required. You can see this dialog below.

    2017-02-21 14_10_50-Export Data-tier Application 'PartsUnlimited'

    Once you’ve chosen a location, you click next and get a summary page.

    2017-02-21 14_13_31-Export Data-tier Application 'PartsUnlimited'

    Click finish, and the process starts. This runs very quickly, extracting the schema and then all the data, noting the results of each object.

    2017-02-21 14_13_38-Export Data-tier Application 'PartsUnlimited'

    That’s it. If I look in the location, I’ll see my BACPAC file.

    2017-02-21 14_15_11-DAC Packages

    I can copy this to another machine and import it to recreate a database. We’ll do that in another post.

    SQLNewBlogger

    A short, quick post. I’ve done this before, but I had to do this for a quick process and took 5 extra minutes to take screen shots, spending 5 minutes later writing this up.