CREATE PROCEDURE emp_summary
@.emp_id int ,@.start_date datetime=0,@.end_date datetime=0
AS
SET NOCOUNT ON
IF @.start_date=0 AND @.end_date=0
BEGIN
SET @.end_date=getdate()
SELECT *
FROM emp WHERE emp_id_id=@.emp_id AND a.join_date>@.start_date AND a.joindate<=@.end_date
END
ELSE
SELECT *
FROM emp WHERE emp_id_id=@.emp_id AND a.join_date>@.start_date AND a.joindate<=@.end_date+1
GO
This is the Stored procedure i wrote to get the emp summary with date range and with no date ranges.If i pass start_date and end_Date Sp executes 'else' part if dont pass the parameters it execultes 'IF' part.Can i optimize this SP further?I'd use:CREATE PROCEDURE
@.emp_id INT
, @.start_date DATETIME = NULL
, @.end_date DATETIME = NULL
AS
SELECT *
FROM emp
WHERE emp_id = @.emp_id
AND join_date BETWEEN Coalesce(@.start_date, join_date) AND Coalesce(@.end_date, join_date)
RETURN
GOIf you can't afford the table scan, I'd resort to using dynamic SQL, but I'd try this first.
-PatP
Monday, March 26, 2012
optimize the stored procedure
Labels:
create,
database,
datetime0,
datetime0asset,
emp_summaryemp_id,
end_date,
end_date0beginset,
int,
microsoft,
mysql,
nocount,
onif,
optimize,
oracle,
procedure,
server,
sql,
start_date,
start_date0,
stored
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment