r/SQL • • Sep 18 '26

SQL Server Newer to SQL and recently inherited code

Might not be a standard request but does anyone have any best practices for interpreting other people's code if they don't leave a fair amount of notes?

6 Upvotes

22 comments sorted by

7

u/Eleventhousand Sep 18 '26

Break it into chunks and try to understand some of the individual pieces. How is it structured? Does it have a bunch of nested subqueries and derived tables? If so, it can often be more difficult to work inside out like that. If they at least broke everything out into CTEs, it can often be easier to understand.

2

u/Thatgiuknow Sep 18 '26

There are a substantial amount of CTEs in the query. The big issue we're having is that the data doesn't match the manual version we run (we expect some variance) however parts of the data are wildly off. I'll have to check what's going on in the CTEs to get started. thank you!!

2

u/reditandfirgetit Sep 18 '26

Split out the ctes into tables and examine the data at each step and eithervcertifyvits correct or document the issue. I would try to document all thr issues before trying to fix things unless its a blatantly obvious fix for the variance

1

u/lalaluna05 Sep 18 '26

This is how I do it. I don’t love LLMs for this because I want to understand and follow the flow.

2

u/BigBagaroo Sep 18 '26 edited Sep 18 '26

No, use AI for this! I recently inherited some old stuff, and AI has been a godsend in doing those boring comparisons. Create scripts, query the sources, comparing the data etc is what you should use AI for, in my humble opinion!

1

u/BigBagaroo Sep 18 '26 edited Sep 18 '26

Could be due to a non-deterministic order of the data. Having at least some CTEs is a blessing here.

Consider using an AI to figure out the differences, this is what it excels at.

1

u/Comprehensive-Tea-69 Sep 25 '26

Oh you’re fortunate- CTEs will make it MUCH easier to figure out what’s happening. That’s one of the big reasons to use them

4

u/LetsGoHawks Sep 19 '26

Reformat it to your standard. You end up learning a lot.

Caveat: Almost everything I've inherited has been so poorly formatted it's ridiculous.

2

u/lalaluna05 Sep 18 '26

I have a hard time looking at a wall of code especially if their formatting is…dense and there are a lot of subqueries etc.

I will do one of two things (sometimes both):
I dump it into something like notepad++ then I’ll open another blank tab and work my way down re-writing and adding notes.
Or I will literally print it out and use a highlighter and just highlight objects, tables, aliases, etc and follow it down.

It’s not really conventional I guess but it works for my brain, especially when it’s just a dense, complex script.

2

u/gakule Sep 18 '26

Recently used Claude (Opus 4.8) to interpret the differences between a few different pulls that should tie out (with some rounding variance) and recommend some pathways to fix the one that was wrong. It did a bang-up job, honestly it's a great use.

1

u/PalpitationKind8854 Sep 18 '26

Sure. Comments are good. If I use something daily I'd make a view if I can't rewrite it myself for whatever management level reason.

1

u/ShadowDancer_88 Sep 19 '26

Every big problem is just a bunch of little problems in a trenchcoat.

I personally hate the formatting most other people slop together, the first thing I do when trying to understand a stored procedure is to reformat into a format I like and understand.

Once you have stopped trying to play with other people's crap formatting, go through it chunk by chunk, and do not use AI. If you use AI, you will never learn to do it yourself.

Once you actually understand what each CE, select, update, insert, etc. does, put in the notes/comments.

When you, and not an LLM, understand all the little bits, you'll also understand the architecture of the tables, and will be able to truly understand the big picture.

Once you have that, you'll know where to start when troubleshooting why data doesn't line up, why you're getting result x instead of y, and when you need to write a custom query/update/delete/insert for whatever reason, you'll understand the consequences of each sort of change, which an LLM can never do.

1

u/ggnndd12 Sep 20 '26

Any SQL that isn’t immediately apparent what it does was written by a madman. Run for the hills.

1

u/Glitch_In_The_Data Oct 18 '26

You can rely on AI for this. Basically give the code and ask the LLM to explain in simple terms. We use Genie for both understanding the existing code… assessing the impact and even optimising the code for performance improvements.

1

u/mrpostitman Sep 18 '26

if the confusing bits aren't annotated, it could be anything from exploiting optimizer quirks to simply badly written code.

LLMs can be a good shortcut, but there's good learning to be had from good old fashioned restructuring, annotating, and just playing with chunks until it makes sense. making ugly code legible is hugely helpful going forward, and not a bad way to learn idioms you hasn't come across.

0

u/angryapathetic Sep 18 '26

LLM as a first stage for understanding what the whole procedure is intended to do and the basic steps

Then dig into to whatever is highest priority in terms of your role - what breaks most often or takes longest to run

0

u/BaddDog07 Sep 18 '26

pop it into an llm, if you need to change something dont assume the llm is correct until you have a good understanding of the context around the data

0

u/joleshole Sep 18 '26

Claude or chat gpt

-1

u/KBradl Sep 18 '26

LLms can often help identify what the queries intention is.

I try to separate out areas and document them myself for my understanding

Once its contained and understood I often break them out into sections like:

------------------------ AREA ONE: This areas does this

-------------------------END AREA

0

u/Candid_Finding3087 Sep 18 '26

Yeah I use llms to understand my own sql months later. They’ve been pretty good at recognizing the structure and intent and squaring that with comments in the script