Daily Coping 25 Dec 2020

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 stop for a minute today and smile while you remember a happy moment in 2020.

I’ve had more than my share this year, but my happy moment from 2020 that comes to mind is from February. The only airplane trip of the year for me, to celebrate a birthday.

20200131_172440

Merry Christmas.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 25 Dec 2020

All I Want for Christmas

I’ve been learning to play All I Want for Christmas this fall, hoping to have some fun with the family during the holiday. Hopefully it’s a festive, fun time of year for many of you out there, despite the challenges of this pandemic year. As I was listening to the words, I thought about what I’d want this year.

2020 has been disruptive to me, as someone that often travels and meets lots of people. Trying to comply with regulations, as well as stay safe, has meant parts of my job have changed this year. One thing that hasn’t changed is that I still work most of the time in my home office, which is a change that many of you got to experience this year. I wasn’t sure how people would cope, but most of the friends and colleagues I speak with have enjoyed working at home.

While I do think the world of work for technology people has likely changed, I also suspect that some of you won’t get to keep working at home forever. There are certainly managers that don’t like this format, though I hope this year has opened their eyes to understanding just how productive people can be, even when not visually supervised.

One of my wishes for Christmas is that we find a way to hold some live events next year, with speakers and attendees getting to be in the same room, network, and shake hands. I know this won’t happen soon, but I’d happily skip other presents if this would come true sometime in 2021.

I suspect that many people this year have varied wishes for this holiday season, mostly for the health and well-being of their families, but perhaps some other more tangible hopes as well. In any case, I wish you joyous celebrations and smiles with loved ones.

Steve Jones

Posted in Editorial | Tagged | Comments Off on All I Want for Christmas

Daily Coping 24 Dec 2020

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 Give away something you have been holding on to.

I have made more donations this year and in the past,. Partially I think this is because life slowed down and I had time to clean out some spaces. However, I have more to do, and when I saw this item, I decided to do something new.

I’m a big supporter of Habitat for Humanity. During my first sabbatical, I volunteered there quite a bit, and I’ve continued to do that periodically since. I believe shelter is an important resource most people need. site:I’ve had some tools at the house that I’ve held onto, thinking they would be good spares. I have a few cordless items, but I have an older miter saw and a table saw that work fine.

Habitat doesn’t take these, but I donated them to another local charity that can make use of them. I’m hoping someone will use them to improve their lives, either building something or maybe using them in their work.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 24 Dec 2020

Basic Cursors in T-SQL–#SQLNewBlogger

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

Cursors are not efficient, and not recommended for use in SQL Server/T-SQL. This is different from other platforms, so be sure you know how things work.

There are places where cursors are useful, especially in one-off type situations. I recently had a situation, and typed “CREATE CURSOR”, which resulted in an error. This isn’t valid syntax, so I decided to write a quick post to remind myself what is valid.

The Basic Syntax

Instead of CREATE, a cursor uses DECLARE. The structure is unlike other DDL statements, which are action type name, as CREATE TABLE dbo.MyTable. Instead we have this:

DECLARE cursorname CURSOR

as in

DECLARE myCursor CURSOR

There is more that is needed here. This is just the opening. The rest of the structure is

DECLARE cursorname CURSOR [options] FOR select_statement

You can see this in the docs, but essentially what we are doing is loading the result of a select statement into an object that we can then process row by row. We give the object a name and structure this with the DECLARE CURSOR FOR.

I was recently working on the Advent of Code and Day 4 asks for some processing across  rows. As a result, I decided to try a cursor like this:

DECLARE pcurs CURSOR FOR SELECT lineval FROM day4 ORDER BY linekey;

The next steps are to now process the data in the cursor. We do this by fetching data from the cursor as required. I’ll build up the structure here starting with some housekeeping.

In order to use the cursor, we need to open it. It’s good practice to then deallocate the objet at the end, so let’s set up this code:

DECLARE pcurs CURSOR FOR SELECT lineval FROM day4 ORDER BY linekey;
OPEN pcurs
...
DEALLOCATE pcurs

This gets us a clean structure if the code is re-run multiple times. Now, after the cursor is open, we fetch data from the cursor. Each column in the SELECT statement can be fetched from the cursor into a variable. Therefore, we also need to declare a variable.

DECLARE pcurs CURSOR FOR SELECT lineval FROM day4 ORDER BY linekey;
OPEN pcurs
DECLARE @val varchar(1000);
FETCH NEXT FROM pcurs into @val
...
DEALLOCATE pcurs

Usually we want to process all rows, so we loop through them. I’ll add a WHILE loop, and use the @@FETCH_STATUS variable. If this is 0, there are still rows in the cursor. If I hit the end of the cursor, a –1 is returned.

DECLARE pcurs CURSOR FOR SELECT lineval FROM day4 ORDER BY linekey;
OPEN pcurs
DECLARE @val varchar(1000);
FETCH NEXT FROM pcurs into @val
WHILE @@FETCH_STATUS = 0
BEGIN
...
FETCH NEXT FROM pcurs into @val
END
DEALLOCATE pcurs

Where the ellipsis is is where I can do other work, process the value, change it, anything I want to do in T-SQL. I do need to remember to get the next row in the loop.

As I mentioned, cursors aren’t efficient and you should avoid them, but there are times when row processing is needed, and a cursor is a good solution to understand.

SQLNewBlogger

As soon as I realized my mistake in setting up the cursor, I knew some of my knowledge had deteriorated. I decided to take a few minutes and describe cursors and document syntax, mostly for myself.

However, this is a way to show why you know something might not be used. You could write a post on replacing a cursor with a set based solution, or even show where performance is poor from a cursor.

Posted in Blog | Tagged , , | 3 Comments