473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to avoid redundant case statements in a stored procedure

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
6 1789
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
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
Thanks Plamen for the advice!

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

-Emily

Dec 18 '06 #4
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nipon | last post by:
Hi, I am using MS SQL Server 7.0 SP2 in Windows 2000 server SP4. I have one-to-many tables (TABLE_HEAD and TABLE_DETAILS)which I am going to update by using a stored procedure with UPDATE...
0
by: jamiemcc | last post by:
Hi, I would like to have 1 stored procedure call another stored procedure (which contains multiple select statements) and then be able to access the 3 result sets. Example Create Procedure ....
13
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something...
7
by: Matt Jensen | last post by:
Howdy Fairly simple question I think, I presume the answer is no it can't be reused for 2 *SELECT* statements, but just hoping for clarification. Just asking in the interests of trying to minimise...
9
by: Mark A | last post by:
Quoted from the: "Application Development Guide: Programming Server Applications Version 8.2": (DB2 for LUW). "Stored procedures cannot issue COMMIT or ROLLBACK statements if the stored...
6
by: betbubble | last post by:
I need help on two questions: 1. Is temp table the only way to pass recordsets from a nested stored procedure to a calling stored procedure? Can we avoid temp tables in this case? 2. Are...
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have coded multiple select statements in a single stored procedure, and when I execute this procedure on SQL Server Management Express, I correctly get multiple result sets. But, if I try to add...
4
by: vertigo262 | last post by:
Is it possible to use to select statements in a stored procedure? I am building a movie rating system, what I am doing is creating a table with movies and individual user ratings. The code...
3
by: digz | last post by:
This is a very simplified version of something I am trying to understand. The State object holds the strings and maps , and I pass a reference to State to the process Function which manipulates it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.