Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Troubleshooting SQL Code Using Comments

5.00/5 (2 votes)
3 Feb 2018CPOL1 min read 6.2K  
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)