473,750 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.VWdisPrintR eceipts.UpdateD ate = '') -- report not scanned
THEN 3
WHEN (ri.Status LIKE '%COMPLETE%' AND
( ri.UpdateDate <'' AND ri.UpdateDate <= GetDate()) AND
( dbo.VWdisPrintR eceipts.UpdateD ate <''
AND dbo.VWdisPrintR eceipts.UpdateD ate <= 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.VWdisPrintR eceipts.UpdateD ate = '') -- report not scanned
THEN 'Report not scanned'
WHEN (ri.Status LIKE '%COMPLETE%' AND
( ri.UpdateDate <'' AND ri.UpdateDate <= GetDate()) AND
( dbo.VWdisPrintR eceipts.UpdateD ate <''
AND dbo.VWdisPrintR eceipts.UpdateD ate <= 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 1805
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********@yah oo.comwrote in message
news:11******** *************@7 9g2000cws.googl egroups.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.VWdisPrintR eceipts.UpdateD ate = '') -- report not scanned
THEN 3
WHEN (ri.Status LIKE '%COMPLETE%' AND
( ri.UpdateDate <'' AND ri.UpdateDate <= GetDate()) AND
( dbo.VWdisPrintR eceipts.UpdateD ate <''
AND dbo.VWdisPrintR eceipts.UpdateD ate <= 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.VWdisPrintR eceipts.UpdateD ate = '') -- report not scanned
THEN 'Report not scanned'
WHEN (ri.Status LIKE '%COMPLETE%' AND
( ri.UpdateDate <'' AND ri.UpdateDate <= GetDate()) AND
( dbo.VWdisPrintR eceipts.UpdateD ate <''
AND dbo.VWdisPrintR eceipts.UpdateD ate <= 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********@yah oo.comschreef in bericht
news:11******** *************@7 9g2000cws.googl egroups.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.VWdisPrintR eceipts.UpdateD ate = '') -- report not scanned
THEN 3
WHEN (ri.Status LIKE '%COMPLETE%' AND
( ri.UpdateDate <'' AND ri.UpdateDate <= GetDate()) AND
( dbo.VWdisPrintR eceipts.UpdateD ate <''
AND dbo.VWdisPrintR eceipts.UpdateD ate <= 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.VWdisPrintR eceipts.UpdateD ate = '') -- report not scanned
THEN 'Report not scanned'
WHEN (ri.Status LIKE '%COMPLETE%' AND
( ri.UpdateDate <'' AND ri.UpdateDate <= GetDate()) AND
( dbo.VWdisPrintR eceipts.UpdateD ate <''
AND dbo.VWdisPrintR eceipts.UpdateD ate <= 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********@yah oo.comwrote in message
news:11******** *************@n 67g2000cwd.goog legroups.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
1547
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 statements. But somehow ,ONCE IN A WHILE, when executing the stored procedure with about 1000 rows updated, I lost 10-20 records from TABLE_HEAD (seems like 10-20 records were deleted) , and all data rows in TABLE_DETAILS were updated correctly (even...
0
2114
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 . exec Multiple --manipulate result set A
13
2757
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 like this... On Error Resuse Next MyFoo 123 lngErrNum = Err.Number On Error Goto 0
7
3002
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 code. i.e. if the SqlDataAdapter uses a connection to the one database for a select statement and I want to do a second select statement on exactly the same database, can I reuse the SqlDataAdapter? I ask because I want to put both 'select'...
9
7315
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 procedure was invoked from an application that established a type 2 connection to the database." 1. Is this accurate? 2. If a stored procedure is tested from the CLP (using type 2 connection to
6
3299
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 operations in a stored procedure are treated as a transaction? Any help will be greatly appreciated. Background: We need to use temp table to pass recordsets from a nested stored procedure to a calling stored procedure. Our understanding is
2
5093
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 a new Data Source to to my VB 2005 project, and point to this stored procedure, the data source wizard only sees the 'first' select statement. Is there a way to load multiple tables in a DataSet from a single stored procedure with multiple...
4
6938
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 needs to get a count of the ratings, then the sum of them to get the percentage. I am getting a error Exception Details: System.IndexOutOfRangeException: columncount
3
2059
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 based on the tag passed in. I cannot pass the strings/maps in question as references coz .. a) thats redundant, state has everything, it increases number of parameters unnecessarily b) I have to do the case/switch outside the function to...
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.