Search

musings of extofer

and his secret identity, Gabriel Villa

Category

SQL Server

TSQL Tuesday #20: T-SQL Best Practices

It’s T-SQL Tuesday again, and although I seldom write on Tuesday, I attempt to participate. The brainchild of Adam Machanic (Blog|Twitter), T-SQL Tuesday invites new and existing SQL Server bloggers to post about the same topic on the same day.  This time around, the topic is “T-SQL Best Practices”, hosted by Amit Banerjee (Blog|Twitter)


What is T-SQL, anyway?
T-SQL, or Transact-SQL, is Microsoft and Sybase’s proprietary extension to SQL.All applications that talk to a SQL instance use T-SQL statements to talk to the server, regardless of the user interface or the application.

T-SQL is a proprietary top-down procedural programming language. It was originally developed jointly by Microsoft and Sybase for Sybase SQL Server on UNIX until 1993. From that point forward, Microsoft SQL Server was developed for NT Server. T-SQL is not an object-oriented programming language with object or methods and it does not compile into binaries. It is, however, a Server Side processing code used to query data. When I develop T-SQL, I treat it as any other programming language.

For T-SQL best practices, I recommend using programming guidelines similar to those of C#, Java or VB.Net.

User Defined Functions
I have always been a fan of functions. To “bring out the Fun in Functions”, I learned that in any programming language, if you write it more than once, write a function; if you write it three times, stop programming and start a new career. In T-SQL, a UDF can return a single value (Scalar Function) or return a set of data (Table Value).

Use naming conventions
Use common naming conventions to name your variables. This standardization allows any programming to read your code and understand what is being processed. First, figure out the purpose of the variable, then give the variable a precise name, and finally, imply its Data Type. For instance, the date variable @BirthDate [datetime] is easy to understand. However, if you named it @BornColumn [datetime], somebody down the line would be thinking “Huh?”It would not be so interpretive.

Legible Code
Like most word processing programs, SSMS will automatically wrap your lines of code we all write lengthy blocks of code if you don’t press at certain points you could theoretically have only one long line of code the best thing to do is to turn off word wrap for better legibility to do this, select Tools, Options, Text Editor, Transact-SQL, under Settings Turn off Word Wrap for legibility.

Commenting Code
In T-SQL, make concise comments describing the stored procedure or trigger. Best practice for commenting code is to make it understandable when you revisit this code years later. Dates and initials on comments can also alleviate problems.
There are two ways to comment code in T-SQL:

— Two hyphens create one line for commenting
GO
CREATE PROCEDURE sp_TSQL2sDay

/* This is a block of comments

The end delimiter of this comment
is an asterisk and front slash */
GO
ALTER PROCEDURE sp_TSQL2sDay

Version Control
SVN, Mercurial, Git, and SourceSafe are a few well-known version control systems to apply to source code. Although how do you source control T-SQL? It is not common to build the scripts in a programming IDE (Integrated Development Environment). Red-Gate has a product to source control schemas and data, but T-SQL saves in the database as a Server Side Script. I don’t use Red-Gate’s product, but I recommend that you save the scripts, whether stored procedures, triggers, or UDF, as a .sql file, and add them to your version control of choice.

If ifs and buts were candy and nuts, we’d all have a Merry Christmas. If pigs could fly, we’d all need stronger windshield wipers….As much as I wish this could be a feature rich post on how to create a UDF or implement version control on .sql files, it’s not. My sincere hope is that this will soon lead to that. In the meantime, these are just a few of my most recommended rules of thumb when developing T-SQL.

How I Overcame Epic Fail

FAIL STAMP

You can’t avoid mistakes.  It takes a seasoned programmer to admit they’ve had their share of epic fails. I think I do well, as my boss exclaimed “You’re at the Real McCoy now!” … meaning (I think) I’m not working for internal clients anymore.   This is my first job working for an ISV (Independent Software Vendor) since I began programming.

While the wounds are fresh, I’d like to share some of my greatest challenges and how I overcame them.

Being a Software Consultant
I never thought working for myself and making my own hours writing code, wearing pajamas in the room next to where my one and three year olds slept would be a problem. Truth was, although you can actually make great money working from home, you don’t get the experience from team building, code reviewing or even source control.
I had to struggle to get out of my independent coder cocoon. I did this, at least in the inception of .Net, by going to TechNet events and listening to a broad array of developing with .Net sessions.  I also joined an AITP chapter and a Linux User Group. At that time I also learned about the open source community, and how you could share your projects with other users.  I started to interact more and more with developers on Internet forums. It seemed like I had co-workers at my disposal.  With a few clicks, an email or instant message could help me when I got myself into a tight spot.  I learned advanced techniques only by sharing with other developers. The greatest benefit I got out of this was learning how to manage time, project and money. This allowed me to take a position in management for the first time.

Falling Behind
Being a manager was not all it was cracked up to be. I thought I could come in fashionably late and leave as I pleased. I could, but the truth was, there were tasks to be done and staff to oversee. As business grew, the responsibilities increased. Instead of working less and making more, the norm was making less and working more. Weekly travel, hours on Saturday, and on-call on Sunday left me no free time. My greatest challenge was keeping up with the ever changing technology. I wasn’t programming on a daily basis. Implementing new systems meant hiring a contractor to take on a project for three to six months. I was no longer involved and I was no longer happy.
I left the management position to pursue what is dear to me, writing code. It’s hard to do a comeback.  It’s not impossible. Take for instance, Robert Downey, Jr.  He was a wreck and now he’s Iron Man. I knew I could do the same but I had a lot of catching up to do.  I’d missed several versions of Visual Studio .Net and didn’t even realize they dropped the “.Net” from it. SQL Server was totally different from 2000. I had to move quickly as a company took a chance on me even though my knowledge was a little outdated. The company gave me six months to progress.  I had to redo some .Net 2003 and take it to the next level.  Rather than playing it safe with 2005, I chose 2008. I also had to convert from my comfort zone of Visual Basic to C#.   I challenged myself to do this in three months rather than six.

How To Catch Up
I looked long and hard at what was around me. I knew Visual Studio 2003, but not as well as I knew Visual Studio.Net. After playing some catch-up on Visual Studio 2008 and SQL Server 2008 for two months, I registered for an exam to be taken a month later so I could still meet my three month challenge. The most important thing I did was to schedule and register for the exam. I know people who want to certify and study to certify, but it’s when you’ve paid money and scheduled a date that you’ve created yourself a real deadline.  
Still cloudy on the subject, I created a study plan.  I also found much help and resources on Twitter. Social Media was evolving, and virtual user groups and #sqlhelp hashtags were mentioned regularly on Twitter.  I became family with Twitter, again, creating a network of go-to experts that could get me out of a jam.  I found free training and learned about branding myself.
Maybe the stint in management could count for something. I worked hard at my career and planned it as a project with goals, and certifications served as my benchmarks.  I became certified in 2009 as an MCTSand achieved MCPD status in 2010. 
Now I plan on staying on the certification track.   I won’t allow my certs to expire and I vow to upgrade to newer technologies one day at a time.

Meme Monday: 04042011

I saw on the twittersphere this morning that Thomas LaRock (blog | @SQLRockstar) began Meme Monday today, “Write a SQL blog post in 11 words or less”. So I jumped in, answering a question I was asked at my last presentation on SQL  Security, can you change the SA user name:


“use master go alter login sa with name = notsausername” Nough Said!


Since most people are tagging others to participate, I challenge 
Bill Fellows | @billinkc
Meredith Ryan-Smith | @coffegrl
Kelly Martinez | @greeleygeek 
to follow

Denver Presentation at Rocky Mountain Tech Trifecta

I’m speaking this Saturday at the Rocky Mountain Tech Trifecta in Denver’s Tech Center. It is the first talk I do outside my comfort zone of Colorado Springs, not to mention at a large event. Sold out with six hundred registered attendees and over 80 on reserve trying to get in and 47 talks. Below is my slide deck for my presentation “Securing your SQL Servers”, and lastly, please rate this session if you attend.

I’d appreciate your honest evaluations, in hopes these critiques will assist me to deliver improved sessions as time goes by.

SQL Saturday #66, Colorado Springs

100_0659
Ted Malone presenting on Application Lifecycle

This weekend was my first time at a SQL Saturday. SQL Saturday is a free, one day – Saturday, training event put together by volunteers and sponsors. The event is for IT professionals that deal with Microsoft SQL Server and want to learn more on the topics presented by speakers. This weekend, the Colorado Springs SQL Server User Group, along with some friends from Denver and sponsors, put on the very unique event.

This event in Colorado was held at an indoor family fun center, fully equipped with an indoor Go Kart race track, mini-golf, laser tag, video games and more. Attendees were provided with game cards at which we used at breaks to, well, compete with each other in these realms. It was a great method to be social with otherwise complete strangers sharing an interest in SQL Server that Saturday. Oh, did I mention the sessions on SQL Server.

The morning began with networking, an interesting meet and greet session to break the ice. Then the sessions began. There were 15 sessions put into three tracks, enough to overwhelm anyone, beginner or experienced alike. I was fortunate to attend sessions by Chris Randall, Meredith Ryan-Smith, Randy Knight and Ted Malone. I was also very happy to meet in person and make some new friends:

100_0654
PASS Regional Mentor, TJay Belt

Bill Fellows | @billinkc
Chris Randall | @cfrandall
Meredith Ryan-Smith | @coffegrl
Randy Knight | @randy_knight
TJ Belt | @tjaybelt
Bill Pearson | @Bill_Pearson
Steve Jones | @way0utwest
Doug Lane | @douglane4
Kelly Martinez | @greeleygeek

Aside from this being my first SQL Saturday as an attendee, this was my first as a speaker as well. I took the liberty to present on the subject of SQL Server Security. I await to receive constructive criticism so I can improve as a speaker, and be able to present more on the subject. I also posted my slide deck on a previous post if interested. I can not compare to other SQL Saturdays as an attendee or speaker, but I will not forget the atmosphere, hockey theme, laser tag battles, mini-golf, new acquaintances and overall, fun.

Thank you to all the hard working organizers, volunteers and sponsors for putting on a great event.

Securing your SQL Server

Become aware of some commonly overlooked practices in securing you SQL Server databases. Learn about physical security, passwords, privileges and roles, restricting or disabling system stored procedures and preventative best practices. And most importantly, discuss the most commonly used security threat: SQL injection and learn how to prevent them.

Create a free website or blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started