Never Just Copy Code

When I’m learning something new that involves coding, I’m always grateful when working code examples are provided. Whether I’m working on training modules, looking up how to accomplish a specific task, or troubleshooting code. While I try to learn what I can from reading, I’m much more of a hands-on person and learn best getting my hands dirty, so to speak.

However, I very much understand there are pitfalls for using these code examples. Namely, it is very easy to simply just copy/paste the code and that’s it. If it works, all good. But do you actually know what the code does? If it breaks or doesn’t perform as expected, can you troubleshoot it? If you wanted to alter it to behaving differently, can you? This is why I press on myself to never just copy code.

Whenever I copy code, I make a habit of breaking it down. I make sure I understand every part of the code, what the code does, and how it does it. I want to make sure I know what I’m doing and learning from the code example so I can further improve my own knowledge.

For example, I’ve never worked with asynchronous programming prior to writing my project Discord bot. I was fortunate that the Discord .NET community has really great documentation on how to get started. with all the necessary code. When I was following the documentation and using their code, I made sure to break down each line to make sure I knew what I was writing. This even lead me down a rabbit hole of reading the Microsoft documentation for understand what asynchronous programming is.

I’ll be honest, I’d have no idea how to troubleshoot my bot if I ran into any issues. Not going to say I’m an expert, but I definitely learned a lot about asynchronous coding.

It’s always good to keep learning.

SQL : “String or binary data would be truncated” Error

While supporting a number of MS SQL servers that I took over as DBA this year, I ran into a scheduled job that handles a nightly data feed started failing. After reverse engineering exactly how this job worked, I found a specific stored procedure failing with the “String or binary data would be truncated” error.

What does this mean? Basically, the process is trying to run an insert query and is trying to insert data into a column that is not big enough. For example, say I have a column defined as type nvarchar(3) and I’m trying to insert ‘testing’ into that column. This will generate this error as ‘testing’ is 7 characters and that column is limited to 3.

Solution! I have two options, ignore the error and let SQL truncate the data; or I can find the offending data increase the size of the destination column.

Ignoring the error by adding “SET ANSI_WARNINGS OFF” to the query will get me around the error; but be warned it will truncate the data and is not recommended.

This leaves increasing the size of the destination column. However, when my source data has over 100k rows of data and my destination table has over 35 columns, that is easier said then done. I could just increase the size of every column in my destination table; but that is overkill in my case. I know there are likely more elaborate ways of going about finding the offending data; but due to time, I needed to find the offending data quick. While this query is very simple, it worked perfectly to help me find the offending data:

select top 1 COLUMN
from table t
order by len(COLUMN) desc
Just changed “COLUMN” to the name of each column of data I need to check until I found all offending data. Very simple and easy to tweak as needed. I found plenty of complex solutions; but sometimes all it takes is a simple solution.