sql server - After adding a function to a stored procedure, the execution time is way too long -


i have stored procedure this.

alter procedure [dbo].[ibs_fetchreqvehicleinpodium]    @locid integer=null begin    set nocount on     select          t.tbarcode, t.plateno, t.delecode               transaction_tbl t              [status] in (3,4)         , locid = @locid         , dtime >= getdate()-7         or ([status] = 5 , datediff(n, cast(deldate datetime), getdate()) <= 3             , locid = @locid , dtime >= getdate()-7)      order          paydate end 

also want check each tbarcode status , calculate time difference, wrote function this:

alter function [dbo].[krrttime](@status numeric(18,2), @cardid varchar(50))  returns int  begin    declare       @requestedtime datetime,      @currentdate datetime,      @keyroomdate datetime,      @krrttime int     if @status= 3     begin       select @requestedtime = t.paydate        transaction_tbl t        t.tbarcode = @cardid        select @currentdate = getdate()        select @krrttime = datediff(minute, @requestedtime, @currentdate)    end     if @status = 4 or @status = 5    begin       select @requestedtime = t.paydate        transaction_tbl t        t.tbarcode = @cardid        select @keyroomdate = t.keyroomdate        transaction_tbl t        t.tbarcode = @cardid        select @krrttime = datediff(minute,@requestedtime,@keyroomdate)    end     return @krrttime end 

then called function in stored procedure.. after adding function in stored procedure... execution time of stored procedure taking long time

if execute stored procedure without function s executing fast..

i don't have database , didn't post exact table structure, either - this:

alter procedure [dbo].[ibs_fetchreqvehicleinpodium]    @locid integer = null begin    set nocount on     -- i'd careful getdate()-7 - "7" subtract?     -- days, months, years? i'd rather explicit it!    declare @todayminus7days datetime     -- assuming mean "today minus 7 days" here .....    set @todayminus7days = datediff(days, -7, getdate())     select          t.tbarcode, t.plateno, t.delecode,        -- use case here - depending on status, return 1 or other datediff        case           when [status] = 3               datediff(minute, t.paydate, getdate())           when [status] in (4, 5)               datediff(minute, t.paydate, t.keyroomdate)           -- guessing here - if status neither 3, 4, nor 5 - return 0 in case - adapt needed!           else 0              end yourdatediff  -- give meaningful column alias here               dbo.transaction_tbl t              ([status] in (3,4) , locid = @locid , dtime >= @todayminus7days)        or         ([status] = 5 , datediff(n, cast(deldate datetime), getdate()) <= 3 , locid = @locid , dtime >= @todayminus7days)    order          paydate end 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -