dbTalk Databases Forums  

Help optmizing a stored proc

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Help optmizing a stored proc in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
CK
 
Posts: n/a

Default Help optmizing a stored proc - 03-09-2006 , 11:45 AM






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



Reply With Quote
  #2  
Old   
figital
 
Posts: n/a

Default Re: Help optmizing a stored proc - 03-09-2006 , 12:00 PM






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.


Reply With Quote
  #3  
Old   
CK
 
Posts: n/a

Default Re: Help optmizing a stored proc - 03-09-2006 , 12:01 PM



Sweet! Thanks man.

"figital" <mharen (AT) gmail (DOT) com> wrote

Quote:
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.




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.