Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
alfatat
Starting Member
1 Post |
Posted - 2006-03-25 : 05:10:12
|
| Hi! Guys I am pasting the code of a function which is taking more than 3 mins to get executed.CREATE FUNCTION dbo.getCOStatus (@ooid as integer ) RETURNS decimal AS BEGIN declare @Status as decimaldeclare @qtyo as decimaldeclare @qtyd as decimalselect @qtyo = sum(cod.quantityOrdered) from codetails cod inner join coheader ch on ch.coid = cod.coid Inner join ooheader oh on ch.coid = oh.coid Where ooid = @ooidselect @qtyd = isnull(sum((p.CartonTo - p.Cartonfrom + 1)* p.CartonQty),0)from PakingListDetails p Inner Join codetails cod on cod.CODetailID = p.CODetailIDInner Join coheader ch on ch.coid = cod.coidInner join ooheader oh on ch.coid = oh.coid Where ooid = @ooidif (@qtyd = 0) set @Status = 3 -- OpenElse if (@qtyd > 0) Begin if (@qtyo < @qtyD) set @Status = 2 -- Closed else if (@qtyo - @qtyD = 0) set @Status = 0 -- Closed else set @Status = 1 -- Partially Completeendreturn @StatusENDthere are around 5000 rows in each of the tables employed by the queries.Just get back to me as soon coz my application is imp. |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-03-25 : 06:02:17
|
I dont know whether above the output but you can just check the script for the Case when query .. .. directly in the query instead of using if codition after that CREATE FUNCTION dbo.getCOStatus (@ooid as integer )RETURNS decimal ASBEGINdeclare @Status as decimaldeclare @qtyo as decimaldeclare @qtyd as decimalselect @Status = Case When (sum(cod.quantityOrdered)) = 0 Then 3 --Open When (sum(cod.quantityOrdered)) > 0 Then Case When ((sum(cod.quantityOrdered)) < isnull(sum((p.CartonTo - p.Cartonfrom + 1)* p.CartonQty),0)) Then 2 -- Closed Case When ((sum(cod.quantityOrdered)) - isnull(sum((p.CartonTo - p.Cartonfrom + 1)* p.CartonQty),0)) = 0 Then 0 -- Closed Else 1 -- Partially Open End End from codetails codinner join coheader ch on ch.coid = cod.coidInner join ooheader oh on ch.coid = oh.coidInner Join PakingListDetails p on cod.CODetailID = p.CODetailIDWhere ooid = @ooidreturn @StatusEND Hope this works for you.If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2006-03-27 : 07:31:44
|
| Can you post the execution plan of the function from a sample run. Indices (or lack thereof) usually are the problem. |
 |
|
|
|
|
|