Home All Groups Group Topic Archive Search About

How to avoid redundant case statements in a stored procedure

Author
18 Dec 2006 2:30 AM
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

Author
18 Dec 2006 3:54 AM
Plamen Ratchev
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


Show quote
"Fir5tSight" <fir5tsi***@yahoo.com> wrote in message
news:1166409031.699969.65450@79g2000cws.googlegroups.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
>
Author
18 Dec 2006 2:17 PM
Fir5tSight
Thanks Plamen for the advice!

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

-Emily
Author
19 Dec 2006 3:30 PM
Fir5tSight
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
Author
19 Dec 2006 9:27 PM
Plamen Ratchev
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


Show quote
"Fir5tSight" <fir5tsi***@yahoo.com> wrote in message
news:1166542255.505163.86820@n67g2000cwd.googlegroups.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
>
Author
19 Dec 2006 9:40 PM
Fir5tSight
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
Author
18 Dec 2006 5:48 AM
Cor Ligthert [MVP]
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

Show quote
"Fir5tSight" <fir5tsi***@yahoo.com> schreef in bericht
news:1166409031.699969.65450@79g2000cws.googlegroups.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
>

AddThis Social Bookmark Button