Published on

August 2, 2011

Improving SQL Server Code: A Lesson Learned

As professionals, we all make mistakes and learn from them. In my early days as a network engineer, I had limited knowledge of databases, which led to an embarrassing code review incident. Today, I want to share this story in detail to emphasize the importance of continuous learning and improvement.

Several years ago, while working as a Network Engineer in the United States, I was responsible for managing a large data center with over 40 servers. Our SQL Server database had a table that stored inventory details for all the servers. The table had two columns: ServerID and ServerName.

One day, I was tasked with writing a script to retrieve data from this table and insert it into a new table created by the database administrator. Feeling confident in my SQL skills, I wrote a script using a cursor to select a single row from the table and insert it into the new table. Although the script worked, it took a considerable amount of time to execute.

Here is the script I wrote:

-- Create Table and Populate with Sample Data
CREATE TABLE ServerTable (
    ServerID INT,
    ServerName VARCHAR(100)
)

INSERT INTO ServerTable (ServerID, ServerName)
SELECT 1, 'First Server'
UNION ALL
SELECT 2, 'Second Server'
UNION ALL
SELECT 3, 'Third Server'

-- Creating New Table
CREATE TABLE NewServerTable (
    ServerID INT,
    ServerName VARCHAR(100)
)

-- Insert Logic
DECLARE @Flag INT
SELECT @Flag = COUNT(*) FROM ServerTable

WHILE (@Flag > 0)
BEGIN
    INSERT INTO NewServerTable (ServerID, ServerName)
    SELECT ServerID, ServerName
    FROM ServerTable
    WHERE ServerID = @Flag

    SET @Flag = @Flag - 1
END

SELECT ServerID, ServerName
FROM NewServerTable

-- Clean up
DROP TABLE ServerTable
DROP TABLE NewServerTable

However, things changed when a new Chief Technology Officer (CTO) was appointed, and code reviewing became a requirement for both old and new code. When my script went through a code review, my Team Leader couldn’t help but laugh at it. He then showed me a much shorter and more efficient code.

Here is the improved code:

-- Create Table and Populate with Sample Data
CREATE TABLE ServerTable (
    ServerID INT,
    ServerName VARCHAR(100)
)

INSERT INTO ServerTable (ServerID, ServerName)
SELECT 1, 'First Server'
UNION ALL
SELECT 2, 'Second Server'
UNION ALL
SELECT 3, 'Third Server'

-- Creating New Table
CREATE TABLE NewServerTable (
    ServerID INT,
    ServerName VARCHAR(100)
)

-- Insert Logic
INSERT INTO NewServerTable (ServerID, ServerName)
SELECT ServerID, ServerName
FROM ServerTable

SELECT ServerID, ServerName
FROM NewServerTable

-- Clean up
DROP TABLE ServerTable
DROP TABLE NewServerTable

As you can see, the new code reduced the insert logic to just two lines. While it may seem simple now, it was a significant improvement for me at that time.

What made this whole experience even more valuable was the support and encouragement from my Team Leader. He asked me to demonstrate the incident during our next team meeting. I openly admitted my mistake and thanked him for the opportunity to learn and grow. It was through his guidance and encouragement that I was able to reach where I am today.

The key takeaway from this story is to avoid using cursors and instead utilize the INSERT INTO...SELECT or SELECT INTO logic. These methods are more efficient and can greatly improve the performance of your SQL Server code.

Remember, we all make mistakes, but it’s how we learn from them and strive for improvement that truly matters in our professional journey.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.