How to avoid redundant case statements in a stored procedure

  • Thread starter Thread starter Fir5tSight
  • Start date Start date
F

Fir5tSight

Hi All,

I have a "SELECT" statement in a stored procedure that looks like the
follows:

--------------------------------------------------------------------------------------------------------------
SELECT

CASE WHEN ( ri.Status NOT LIKE '%COMPLETE%' ) -- report not
completed
THEN 1
WHEN ( ri.Status NOT LIKE '%COMPLETE%' AND
ri.UpdateDate = '' ) -- report not printed
THEN 2
WHEN (ri.Status LIKE '%COMPLETE%' AND
(ri.UpdateDate <> '' AND ri.UpdateDate <= GetDate()) AND
dbo.VWdisPrintReceipts.UpdateDate = '') -- report not scanned
THEN 3
WHEN (ri.Status LIKE '%COMPLETE%' AND
( ri.UpdateDate <> '' AND ri.UpdateDate <= GetDate()) AND
( dbo.VWdisPrintReceipts.UpdateDate <> ''
AND dbo.VWdisPrintReceipts.UpdateDate <= GetDate())) -- report
completed, printed, and scanned
THEN 4
END AS 'Status',

CASE WHEN ( ri.Status NOT LIKE '%COMPLETE%' ) -- report not
completed
THEN 'Report not completed'
WHEN ( ri.Status NOT LIKE '%COMPLETE%' AND
ri.UpdateDate = '' ) -- report not printed
THEN 'report not printed'
WHEN (ri.Status LIKE '%COMPLETE%' AND
(ri.UpdateDate <> '' AND ri.UpdateDate <= GetDate()) AND
dbo.VWdisPrintReceipts.UpdateDate = '') -- report not scanned
THEN 'Report not scanned'
WHEN (ri.Status LIKE '%COMPLETE%' AND
( ri.UpdateDate <> '' AND ri.UpdateDate <= GetDate()) AND
( dbo.VWdisPrintReceipts.UpdateDate <> ''
AND dbo.VWdisPrintReceipts.UpdateDate <= GetDate())) -- report
completed, printed, and scanned
THEN 'report completed, printed, and scanned'
END AS 'Reason' -- Reason for report not being reconciled

--------------------------------------------------------------------------------------------------------------

As you can see, the two "CASE" statements are almost redundant except
that the first selects an integer, and the second selects a text
string.

Is there anyway to avoid the redundancy in the two "CASE" statements?

Thanks!

-Emily
 
You can create a nomenclature table for your status codes where you would
have records like this (table columns separated with comma):

1, 'Report not completed'
2, 'report not printed'
....

Then you can use a single CASE statement to select the numeric codes and
join the result with your nomenclature table to return the code/description
pair.

If you do not want to keep a permanent table you can create a temporary
table in your stored procedure to keep nomenclature codes/descriptions.

Regards,

Plamen Ratchev
http://www.SQLStudio.com
 
Hi,

Did you know that this is a dotNet newsgroup. With DotNet is everything used
even Word, Sharepoint, Excel, SQL transact code etc.

However did you know that for your question there are most probably much
better newsgroups around from Microsoft.

Cor
 
Thanks Plamen for the advice!

I will create a temporary table for this in the stored procedure!

-Emily
 
Hi Plamen,

I use a temporary table as you suggest for this purpose, and the good
news is that it works. However, there is a bad news. The two columns
(id & status) I store in the temporary table are added to the grid as
well (probably because I use a "SELECT" statement in the INSERT INTO
the temporary table).

How can I drop these two columns in the temporary table from the grid
display?

I'm sorry if this is not the right forum for this question, as I can't
find the right forum. People here are quite knowledgable about this
question.

-Emily
 
Can you please post how your final SQL code looks like and I will try to
help. You can hide the columns on the grid side but it is not a good
solution as you will bring unnecessary data to the client application.

Regards,

Plamen Ratchev
http://www.SQLStudio.com
 
Hi Plamen,

I've figured out why the columns in the temporary table are added - I
added them when I added the stored procedure myself. It was my fault.

Thanks for your willingness to help anyway!

-Emily
 
Back
Top