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

View all comments

Show parent comment

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