473,508 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help optmizing a stored proc

CK
I have this

CREATE PROCEDURE dbo.cmsGetTaskOrdersAndFunding2
(
@FundingDate SMALLDATETIME,
@BillingContractID INT, -- null for all contracts
@Filter BIT = NULL

)
AS
-- get list of taskorders with their respective fundingtotals as of
specified date
IF @Filter IS NULL
BEGIN
SELECT TO1.TaskOrderID
FROM TaskOrder TO1
LEFT OUTER JOIN
WHERE (@BillingContractID IS NULL OR TO1.BillingContractID =
@BillingContractID)
END
ELSE
BEGIN
SELECT TO1.TaskOrderID,
FROM TaskOrder TO1
WHERE (@BillingContractID IS NULL OR TO1.BillingContractID =
@BillingContractID) AND TO1.Retired <> @Filter
END

RETURN
GO
------------------
Is there a less redundant way to write this? basically @Filter is an
optional parameter, if it isn't present, I want to return all records and if
it is present all records where Retired <> @Filter. Any ideas? Can I wrap
the WHERE clause in an if statement? Or is there a better way?

TIA,
Chris
Mar 9 '06 #1
2 1143
Just one block:

-- get list of taskorders with their respective fundingtotals as of
specified date
SELECT TO1.TaskOrderID,
FROM TaskOrder TO1
WHERE (@BillingContractID IS NULL OR TO1.BillingContractID =
@BillingContractID)
AND (@Filter IS NULL OR TO1.Retired <> @Filter)

Note this last line. If @Filter is NULL, the entire block is ALWAYS
true so "TO1.Retired <> @Filter" doesn't matter. If @filter is not
null, "TO1.Retired <> @Filter" is the part that matters.

Mar 9 '06 #2
CK
Sweet! Thanks man.

"figital" <mh****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Just one block:

-- get list of taskorders with their respective fundingtotals as of
specified date
SELECT TO1.TaskOrderID,
FROM TaskOrder TO1
WHERE (@BillingContractID IS NULL OR TO1.BillingContractID =
@BillingContractID)
AND (@Filter IS NULL OR TO1.Retired <> @Filter)

Note this last line. If @Filter is NULL, the entire block is ALWAYS
true so "TO1.Retired <> @Filter" doesn't matter. If @filter is not
null, "TO1.Retired <> @Filter" is the part that matters.

Mar 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3921
by: dmalhotr2001 | last post by:
Hi, I have an issue with my query. 1. I have 1 stored proc which have execution calls to multiple stored procs within it. 2. I want to wrap that main stored proc in the transaction and...
9
6427
by: Wolfgang Kreuzer | last post by:
Try hard to become familiar with T-SQL. Can anybodey tell me the best way to deal with set's provided by a stored procedure. Til yesterday I thougt trapping set in temp table using INSERT EXEC...
0
7126
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
5
2122
by: Rhino | last post by:
This question relates to DB2 Version 6 on OS/390. Can a (COBOL) stored procedure on this platform do file I/O, i.e. write to a sequential file? I am trying to debug a stored procedure. As far...
1
2789
by: mike | last post by:
If I try and do a "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1" after I have run a stored procedure in DB2 version 7.2, I get the last generated Key before the CallableStatement was executed...
0
1016
by: Mike Kingscott | last post by:
Meant to post in here as well... Hi there, I have a user control that brings back some links from a database based on a combination of querystring parms, so I'm using VaryByParm to...
14
1816
by: Roy | last post by:
Apologies for the cross-post, but this truly is a two-sided question. Given the option of creating Looping statements within a stored proc of sql server or in the code-behind of an .net webpage,...
0
2035
by: balaji krishna | last post by:
Hi, I need to handle the return set from COBOL stored procedure from my invoking Java program. I do not know, how many rows the stored proc SQL fetches.I have declared the cursor in that proc, but i...
5
1654
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hello, I am in serious need of help. I have an ASP.NET application written in C#. I have a page that processes a file on the web server. The page uses a class I created and stored in the AppCode...
0
2553
by: db2user24 | last post by:
I'm trying to invoke a DB2 stored procedure. The stored proc is coded in C and compiled to a shared library, which has been placed in the <DB2 dir>/functions directory. The platform is Linux (using...
0
7123
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...
1
7042
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...
0
7495
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...
0
5627
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,...
1
5052
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...
0
4707
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...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
418
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...

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.