Category: Blog

  • Exporting a Table to CSV in PoSh–#SQLNewBlogger

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

    I saw someone struggling with exporting some data from a table in a CSV, and decided to give it a try. I think there were a few things that were broken, but here is the basic idea.

    The Scenario

    I want to get all the data from a table into a CSV. As a setup, I have a table that looks like this:

    2019-12-02 13_01_11-SQLQuery10.sql - Plato_SQL2017.sandbox (PLATO_Steve (56))_ - Microsoft SQL Serve

    What I’d like is this data in a CSV, with a format like this:

    "CustomerKey","CustomerName","CustomerNameLen"
    "1","Steve","5"
    "2","Andy","4"
    "3","Brian","5"
    "5","Acme, Inc.","10"
    "6","Apple","5"
    "7","IBM","3"
    "1","Steve","5"
    "2","Andy","4"
    "3","Brian","5"

    Let’s make that happen.

    PoSh to the Rescue

    If you have SQL Server and the Powershell module installed, you can use the Invoke-SqlCmd cmdlet. This takes parameters for a query and a server that you want to use. There are other parameters as well, but I’ll keep this simple.

    The parameters I’ll use are a query with three part naming and then an instance. Here is the command:

    Invoke-Sqlcmd -Query "SELECT * FROM [Sandbox].[dbo].[Customer]" -ServerInstance "Plato\SQL2017"

    Now we want to get that data out to a CSV, and we can use the Export-Csv cmdlet for this. For me, I often want to avoid extra work, though in the past, I’d have set the output of the Invoke-SqlCmd to a variable. I don’t need to here, so I can use pipe the output to the cmdlet like this:

    Invoke-Sqlcmd -Query "SELECT * FROM [Sandbox].[dbo].[Customer]" -ServerInstance "Plato\SQL2017" |
    Export-Csv -Path E:\Documents\sql\Customer.csv –NoTypeInformation

    This will create a new file for me, called Customer.csv.

    
    

    2019-12-02 13_15_17-sql

    In here, we have our data.

    2019-12-02 13_01_52-E__Documents_sql__2019Dec02_125737.csv - Sublime Text

    Easy, and quick. You could even type this from the command line, without saving the code.

    SQLNewBlogger

    A quick look at a common task. In this case, I was helping someone, but I’ve had the need to do this myself, and fixing someone else’s code actually taught me something here. I’d use this as a story of solving a problem in an interview with new technology (PoSh).

    Solving the problem was about 10 minutes work, and really, this took about 15 minutes to set up, get working, and clean the code a bit. I also had to change a few of the other issues to make this simpler, but those will be good posts in the future.

  • T-SQL Tuesday Gifts (#121)

    tsqltuesday

    It’s that time of month. I missed last month while traveling, but I wanted to get back in the party. This month, Mala has invited us to talk about the gifts we’ve gotten this year. An easy one for me in 2019.

    Down Under

    The gift for me this year was getting to travel to Australia and New Zealand. I’ve always wanted to go and haven’t been quite able to make it a priority. Last year when I heard we had a Redgate office opening in Australia, I knew this was my chance. We decided to run a few SQL in the City Summit events in 2019 down under and I got to go.

    I was lucky enough to go, and not only attend a few events, but I spent nearly a month in Australia/New Zealand. I spoke at 3 SQL in the City Summits and three SQL Saturdays, which were very enjoyable for me.

    I brought my family along for part of the trip and ended up traveling a little in the middle and taking some holiday. I managed to visit Brisbane, Melbourne, Christchurch, Dunedin, and Queenstown.

    Things went well, and I actually went back to Sydney in September, but just for a couple days. Still, I made the most of it.

    20190926_133919_Burst01

    This was truly a gift for me.

  • Upgrading Windows 10 Home to Pro

    I use Hyper-V and a few other things that require Windows 10 Pro. When I got my new laptop, it had Windows 10 home on it. Not a big deal, as I’ve run into this a few times over the years when buying consumer laptops. I have a Pro key, so I usually just wipe and reinstall the OS.

    That’s not necessary anymore. Certainly there are unwanted programs that I can remove, but I don’t need an OS reinstall. I found a few articles on the Internet that explain this, but it was simple.

    I first booted the laptop and went through initial Windows setup. I made sure Windows Update didn’t have any pending items. Then I hoped the Control Panel with Win+I. If you’ve never used this, the Windows key with various other items is handy. Win+I gives me settings. I typed in activation.

    2019-12-02 11_53_37-Settings

    You’ll see the activation settings, which you can then click.

    I already did this, but on my laptop, I saw Windows 10 Home already listed and licensed. I then saw Upgrade options below it, where you see Update Product Key here. There were two links. One was for the MS store, to purchase an upgrade. The second was the Change product key option listed.

    2019-12-02 11_53_45-Settings

    I chose that one and then it prompted me to enter a key. I did and clicked OK. This immediately set my laptop to Windows 10 Pro and I could enable and activate Hyper-V.

    Windows has greatly smoothed this process over time and I’m looking forward to the ease of the setup system for this machine.

  • HP is the 2019 Laptop Winner

    This is for me, not necessarily for you, but I decided to go with the HP Spectre again. I saw an email from Best Buy over the Thanksgiving weekend, and surprisingly, the HP was listed on sale. US$300 off the normal $1799 price. I checked HP and they had the laptop at $1399, but that was a 512GB SSD instead of 1TB.

    I thought about it for a few days and Sunday, after a little gym time, I decided to just get one. I was lad I did as the item showed sold out when I got come and wanted to check the Windows edition. Apparently more were on sale Monday, but I was glad I just grabbed on.

    It’s a nice machine, and I like the power cord being angled on the rear corner. Much easier (for me) than the side. I also like the power button being on the other corner, as I often accidentally hit the other power button when picking up the machine.

    Contenders

    The big contenders for me were the  Dell XPS, the Lenovo X1 Carbon, and the Yoga 940. These were all the small size and weight, with similar specs. I read a number of reviews, and tried to weigh the importance of differences for me.

    The Dell is really nice, but I bemoan the lack of a USB-3 port. I often use this for a pointer when presenting. While I can certainly have a dongle or adapter, I felt like I’d just prefer a normal USB port. It’s also nice for a quick phone charge if I need it.

    The X1 is a very light machine, and the pointer is very attractive. I’ve missed not having one over the years, as I first thought this was a great idea in the 90s, in the era before trackpads. Ultimately, this was a more expensive machine, and I’m not sure it was worth it. The config I wanted, even on Cyber Monday, would have been $400-500 more (I only lightly repriced it). At Redgate, we try to spend wisely, and I decided to do that.

    Initial Impressions

    The HP is a nice machine. It’s well built, and it reminds me of the previous model I had (actually 2 generations back), but this one seems to have a better trackpad and a nice, solid feel. That previous one has lasted well, and I’m looking forward to this one.

    20191202_114525_HDR

    In using it for a few hours to start installing things, it worked well. I found the keyboard to be smooth and comfortable, and the trackpad certainly works better than the old one. I found the old one to have lots of inadvertent touches, but this one didn’t give me trouble.

    It’s slightly smaller than the older one, with angled rear corners. All inall, a beautiful machine.

    20191202_114505

    The size is similar, but feels much smaller and lighter. Overall, I’m pleased.

    20191202_114516

    I’ll be spending a lot of time on this one in the next week as I prepare for SQL Saturday #910, so I’m sure I’ll tease out any issues soon. So far, I’m pleased, and hoping it performs and lasts as long as the previous one.