SQL Stored Procedures

I need to create a stored procedure and call it from within an Access application to calculate time spent doing a job. User starts a job -- the clock starts ticking -- the user's shift changes and he leaves the job -- next user restarts job and the clock starts ticking again...

I have 4 fields in this table Start Date and Start Time -- and -- End Date and End Time.

I added 2 computed fields to the table:

StartDateTime -- (dateadd(minute,datediff(minute,(-2),[StartTime]),[StartDate]))

EndDateTime -- (dateadd(minute,datediff(minute,(-2),[EndTime]),[EndDate]))

Table Name is JobTime. RunTime is a float field that needs to contain the duration between the two.

I need to call this from within Access.

I'm sort of new to Stored Procedures so any help will be appreciated.

Thanks.
Eileen MurphyIndependent Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
Could you just calculate elapsed time using StartDateTime and EndDateTime then sum the values by Job?  If this sounds like what you're looking for let me know and I'll work on the code required.  I am assuming there is some sort of job identifier in your schema.
0
Eileen MurphyIndependent Application DeveloperAuthor Commented:
That's right exactly. For each start and stop there is a row in the table. The summing is done on a report. The individual lines are screwed up historically because when the previous developer set it up he used separate date and time fields so if a job went over midnight or across days it wouldn't calculate properly.
0
Eileen MurphyIndependent Application DeveloperAuthor Commented:
I just wrote a SQL Query to do the math -- I used CONVERT(VARCHAR,(enddate-startdate),108) and it works perfectly -- now if I can get some help writing a SP to go through all 250,000 existing records to fix the run times -- and then have one to call from within VBA passing the JobID as a variable at runtime -- that would be awesome!
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Brian CroweDatabase AdministratorCommented:
So, at this point I'm a little confused regarding exactly what you are trying to accomplish.  Do you want to populate the startdatetime and startendtime values?  Is it one procedure or two?
0
Eileen MurphyIndependent Application DeveloperAuthor Commented:
Thanks for your response. I want a stored procedure to go through the existing records and recalculate the run times since they're wrong now.

Then I wanted another to be called from within vba to do it for the current job which I would like to pass to the SP as a parameter value.

Does this make sense?
0
Brian CroweDatabase AdministratorCommented:
Adjust any table/column references to your situation:

This query should correct your runtime values as long as you have calculated StartDateTime and EndDateTime correctly

UPDATE JobTime
SET runTime = DATEDIFF(minute, StartDateTime, EndDateTime)

Open in new window


In this case RunTime should be an INT instead of a FLOAT.  If you need more granularity then minutes then change the DATEDIFF interval parameter.

Below is a possible stored procedure to calculate Job runtime:

USE myDatabase
GO

IF OBJECT_ID('dbo.GetJobRunTime') IS NOT NULL
    DROP PROCEDURE dbo.GetJobRunTime
GO

CREATE PROCEDURE dbo.GetJobRunTime
(
	@JobID	INT
)
AS

DECLARE @RunTime INT

IF NOT EXISTS(SELECT * FROM JobTime WHERE JobID = @JobID)
BEGIN
	--JobID not found
	RETURN 0
END

SELECT @RunTime = SUM(RunTime)
FROM JobTime
WHERE JobID = @JobID

RETURN @RunTime

Open in new window


This will return an INT value in the same units as you used to calculate the RunTime (minutes in this case)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eileen MurphyIndependent Application DeveloperAuthor Commented:
Hi BriCrowe - the  StartDateTime and EndDateTime are computed columns and I am unable to use them in yet another computer column, correct?
0
Brian CroweDatabase AdministratorCommented:
Computed columns can be treated like any other columns as they are calculated every time they are referenced and are not stored with the table unless marked as such with the PERSISTED flag.  It is not really necessary to add columns for any of these computations if you just want to include the logic in the stored procedure.
0
Eileen MurphyIndependent Application DeveloperAuthor Commented:
As always -- don't know what I'd do without you folks. I am an independent contractor and really appreciate this site.

Thanks, and best, Eileen
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.