By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 968 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

How to avoid redundant case statements in a stored procedure

P: n/a
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

Dec 18 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
"Fir5tSight" <fi********@yahoo.comwrote in message
news:11*********************@79g2000cws.googlegrou ps.com...
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

Dec 18 '06 #2

P: n/a
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

"Fir5tSight" <fi********@yahoo.comschreef in bericht
news:11*********************@79g2000cws.googlegrou ps.com...
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

Dec 18 '06 #3

P: n/a
Thanks Plamen for the advice!

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

-Emily

Dec 18 '06 #4

P: n/a
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

Dec 19 '06 #5

P: n/a
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
"Fir5tSight" <fi********@yahoo.comwrote in message
news:11*********************@n67g2000cwd.googlegro ups.com...
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

Dec 19 '06 #6

P: n/a
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

Dec 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.