I implement a software application that uses a SQL server database. I’m running a stored proc in a macro I created in my application, however, I cannot get the “StartTime” (datetime field) to update. Here is the code:
ALTER PROCEDURE [dbo].[pAfterFirstRun] ( @scheduleDate datetime ) AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #VehicleId ( VehicleId INT )
INSERT INTO #VehicleId
(VehicleId)
SELECT VehicleId
FROM ResourceDaily
WHERE WorkDay = @scheduleDate
AND ResourceName = 'RUN 1'
AND ResourceCode IN (SELECT ResourceCode
FROM ResourceDailySchedule
WHERE WorkDay = @scheduleDate)
UPDATE ResourceDaily
SET StartTime = '1899-12-30 07:20:00.000'
WHERE WorkDay = @scheduleDate
AND ResourceName = 'RUN 2'
AND VehicleId IN (SELECT VehicleId
FROM #VehicleId)
UPDATE ResourceDaily
SET StartAddress = '568 BUCKS HILL RD',
StartCity = 'WATERBURY',
StartState = 'CT',
StartZipCode = '06704',
StartXCoord = '-73.029938',
StartYCoord = '41.606228'
WHERE WorkDay = @scheduleDate
AND ResourceName = 'RUN 2'
AND VehicleId IN (SELECT VehicleId
FROM #VehicleId)
END
I know this is a strange way to accomplish the job; originally, I didn’t create a temp table. It was all done through one update statement and I kept changing it to see if I can get it to work.
The original update statement included the updates to StartTime, StartAddress, StartCity, StartState, StartXCoord and StartYCoord all in one statement and everything would update except the StartTime field.
I should also mention that if I were to run the UPDATE statement in Management Studio and set the @scheduleDate to ‘5/2/2011’, the update statement will run correctly… it just doesn’t seem to run in the application with the @scheduleDate parameter.
this seems to be data dependent, so run your procedure manually from SQL Server Management Studio, after making these changes to it:
I added 3 debug queries, which will return result sets that you need to look at and understand before you can solve this. I don’t know your application or what you are trying to do well enough to fix this for you, but you need to see what your queries are doing before your can solve it. I wouldn’t be surprised if it ends up being that #VehicleId is empty after the insert into it. If so, look out for time on the @scheduleDate parameter and time on the WorkDay column values not joining properly. Look into Floor a date in SQL server.