Question

i have a storedprcocedure like this:

SELECT

     k.HBarcode, m.make,t.Compl,
    t.plateno,t.self,t.dtime, v.vtype, l.locname,[dbo].[EDTCAL](t.TBarcode)as EDT,  t.locid,t.vtid,t.lsttic,
     c.Colname, te.UniqueName

  FROM transaction_tbl t

  left JOIN KHanger_tbl k ON t.transactID = k.transactID
  left JOIN make_tbl m ON t.mkid = m.mkid 

  left JOIN vtype_tbl v ON v.vtid = t.vtid 
  left  JOIN Location_tbl l ON t.locid = l.locid 

  left JOIN Color_tbl C ON t.colid = c.colid 
  left JOIN Terminals_tbl te ON k.tid = te.tid

 WHERE t.tbarcode = '0213'

my function like this:

ALTER function [dbo].[EDTCAL](@cardID VARCHAR(50)) RETURNS int
as 
begin
declare
@locid int,
@EDT int,
@minEDT int,
@buffEDT int,
@value int

select @locid= t.Locid from Transaction_tbl t where t.TBarcode=@cardID
select @EDT= l.EDT from Location_tbl l where l.Locid=@locid
select @minEDT=l.MinEDT  from Location_tbl l where l.Locid=@locid
select @buffEDT=l.BuffrEDT  from Location_tbl l where l.Locid=@locid 
if (@EDT is null) or (@EDT=0)
begin
select @value=@minEDT+@buffEDT
end 
else
begin
select @value=@EDT+@buffEDT
end
return @value
end

so my stored procedure execution time s slow..so i removed my function from my procedure and i wrote code like this:

SELECT

     k.HBarcode, m.make,t.Compl,
    t.plateno,t.self,t.dtime, v.vtype, l.locname,case when l.edt is null or l.edt =0 then l.minEdt +l.bufferEdt else l.edt + l.bufferEdt end as EDT,  t.locid,t.vtid,t.lsttic,
     c.Colname, te.UniqueName

  FROM transaction_tbl t

  left JOIN KHanger_tbl k ON t.transactID = k.transactID
  left JOIN make_tbl m ON t.mkid = m.mkid 

  left JOIN vtype_tbl v ON v.vtid = t.vtid 
  left  JOIN Location_tbl l ON t.locid = l.locid 

  left JOIN Color_tbl C ON t.colid = c.colid 
  left JOIN Terminals_tbl te ON k.tid = te.tid

 WHERE t.tbarcode = '0213'

*but getting error : Msg 207, Level 16, State 1, Line 4 Invalid column name 'bufferEdt'. Msg 207, Level 16, State 1, Line 4 Invalid column name 'bufferEdt'

i added extra only this code

case when l.edt is null or l.edt =0 then l.minEdt +l.bufferEdt else l.edt + l.bufferEdt end as EDT

Was it helpful?

Solution

Where does tbarcode = '0213' appear in the execution plan? It should be there somewhere. There is probably no advantage to using it. If in the general you want to improve the execution time of the query you might get rid of the udf altogether and get @minEDT+@buffEDT and @value=@EDT+@buffEDT from a case statement based on a join.

Sorry doing this on a phone. The point is you don't need a separate function. Replace the call to the udf with the following: case when l.edt is null or l.edt =0 then l.minEdt +l.bufferEdt else l.edt + l.bufferEdt end as EDT

The point is that your udf does separate selects for every row returned increasing the execution time. You don't need it because the fields are already being returned as part of your select statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top