Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / SQL

Troubleshooting SQL Code Using Comments

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Feb 2018CPOL1 min read 5.8K   1   2
How to troubleshoot SQL code using comments

Introduction

In my day to day job, I create a lot of SQL reports using Oracle SQL Developer. Often, I edit the SQL code in Notepad++, because I find the syntax highlighting to be better in Notepad++; plus I use it for many other editing purposes.

Often, when I’m writing my SQL code, the results produced have additional or too many rows returned, so I started using commenting techniques to simply development. This is what this article is about, simplifying development of SQL code with comments.

Example Code

Here is some example obfuscated code:

SQL
SELECT *
FROM GPA
WHERE GPA_PIDM = stu_pidm.PIDM
AND GPA_GPA_TYPE_IND = 'I'
--AND GPA_GPA_TYPE_IND = 'O'
AND GPA_LEVL_CODE = 'U'
AND GPA_ACTIVITY_DATE = --2017
	(SELECT MAX(X.GPA_ACTIVITY_DATE)
	FROM GPA X
	WHERE X.GPA_PIDM = stu_pidm.PIDM
	AND GPA_GPA_TYPE_IND = 'I'
	--AND GPA_GPA_TYPE_IND = 'O'
)
AND GPA_HOURS_ATTEMPTED = (
	SELECT MAX(X.GPA_HOURS_ATTEMPTED)
	FROM GPA X
	WHERE X.GPA_PIDM = stu_pidm.PIDM
	AND GPA_GPA_TYPE_IND = 'I'
	--AND GPA_GPA_TYPE_IND = 'O'
)
;

Notice for the AND operators I’ve put them individually on a newline. Also, you see after GPA_ACTIVITY_DATE, I have 2017 commented out at the end of the line.

Changing to Outcome GPA

To change my code to Outcome GPA, which is GPA_GPA_TYPE_IND = ‘O’, I simply make this change:

SQL
SELECT *
FROM GPA
WHERE GPA_PIDM = stu_pidm.PIDM
--AND GPA_GPA_TYPE_IND = 'I'
AND GPA_GPA_TYPE_IND = 'O'
AND GPA_LEVL_CODE = 'U'
AND GPA_ACTIVITY_DATE = --2017
	(SELECT MAX(X.GPA_ACTIVITY_DATE)
	FROM GPA X
	WHERE X.GPA_PIDM = stu_pidm.PIDM
	--AND GPA_GPA_TYPE_IND = 'I'
	AND GPA_GPA_TYPE_IND = 'O'
)
AND GPA_HOURS_ATTEMPTED = (
	SELECT MAX(X.GPA_HOURS_ATTEMPTED)
	FROM GPA X
	WHERE X.GPA_PIDM = stu_pidm.PIDM
	--AND GPA_GPA_TYPE_IND = 'I'
	AND GPA_GPA_TYPE_IND = 'O'
)
;

Then just run it to see the result. The change is just uncommenting 3 lines and commenting the lines with “AND GPA_GPA_TYPE_IND = ‘I'”.

Activity Date of 2017

To change the GPA_ACTIVITY_DATE to just 2017, make this simple change:

SQL
SELECT *
FROM GPA
WHERE GPA_PIDM = stu_pidm.PIDM
AND GPA_GPA_TYPE_IND = 'I'
--AND GPA_GPA_TYPE_IND = 'O'
AND GPA_LEVL_CODE = 'U'
AND GPA_ACTIVITY_DATE = 2017
	/*(SELECT MAX(X.GPA_ACTIVITY_DATE)
	FROM GPA X
	WHERE X.GPA_PIDM = stu_pidm.PIDM
	AND GPA_GPA_TYPE_IND = 'I'
	--AND GPA_GPA_TYPE_IND = 'O'
)*/
AND GPA_HOURS_ATTEMPTED = (
	SELECT MAX(X.GPA_HOURS_ATTEMPTED)
	FROM GPA X
	WHERE X.GPA_PIDM = stu_pidm.PIDM
	AND GPA_GPA_TYPE_IND = 'I'
	--AND GPA_GPA_TYPE_IND = 'O'
)
;

I simply uncommented the sub selection statement and uncommented 2017.

No Need for GPA Hours

Let’s say I don’t need GPA_HOURS_ATTEMPTED. The change is also simple:

SQL
SELECT *
FROM GPA
WHERE GPA_PIDM = stu_pidm.PIDM
AND GPA_GPA_TYPE_IND = 'I'
--AND GPA_GPA_TYPE_IND = 'O'
AND GPA_LEVL_CODE = 'U'
AND GPA_ACTIVITY_DATE = --2017
	(SELECT MAX(X.GPA_ACTIVITY_DATE)
	FROM GPA X
	WHERE X.GPA_PIDM = stu_pidm.PIDM
	AND GPA_GPA_TYPE_IND = 'I'
	--AND GPA_GPA_TYPE_IND = 'O'
)
/*AND GPA_HOURS_ATTEMPTED = (
	SELECT MAX(X.GPA_HOURS_ATTEMPTED)
	FROM GPA X
	WHERE X.GPA_PIDM = stu_pidm.PIDM
	AND GPA_GPA_TYPE_IND = 'I'
	--AND GPA_GPA_TYPE_IND = 'O'
)*/
;

As you can see, by formatting my code and using comments at various points, it makes it easy to troubleshoot the SQL code by making minimal changes.

Hope this helps you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Taft College
United States United States
I’m a software developer. Currently I’m working at Taft College as a Programmer.

Comments and Discussions

 
SuggestionNot an article Pin
pt14018-Feb-18 21:41
pt14018-Feb-18 21:41 
Questiontroubleshoot? Pin
dmjm-h6-Feb-18 9:26
dmjm-h6-Feb-18 9:26 
You don't explain how the comments help you troubleshoot.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.