运行存储过程时出现SQL更新问题

时间:2011-05-06 15:32:43

标签: sql sql-server tsql sql-update

我实现了一个使用SQL Server数据库的软件应用程序。我正在我在我的应用程序中创建的宏中运行存储过程,但是,我无法获取要更新的“StartTime”(日期时间字段)。这是代码:

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

我知道这是一种完成这项工作的奇怪方式;最初,我没有创建临时表。这一切都是通过一个更新声明完成的,我不断更改它以确定我是否可以让它工作。

原始更新语句包括对StartTime,StartAddress,StartCity,StartState,StartXCoord和StartYCoord的更新,所有内容都在一个语句中,除了StartTime字段外,所有内容都会更新。

我还应该提一下,如果我在Management Studio中运行UPDATE语句并将@scheduleDate设置为'5/2/2011',则更新语句将正确运行...它似乎似乎没有运行在具有@scheduleDate参数的应用程序中。

3 个答案:

答案 0 :(得分:1)

看起来你正在插入你的表

ResourceName = 'RUN 1'

然后你根据

更新它

ResourceName = 'RUN 2'

我不确定您的数据是什么样的,但这可能是一个问题

答案 1 :(得分:0)

是SmallDateTime专栏吗?如果是这样,最小值是1900年1月1日

答案 2 :(得分:0)

这似乎与数据有关,因此在对SQL Server Management Studio进行这些更改后,请从SQL Server Management Studio手动运行程序:

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)
--debug
SELECT '#VehicleId' AS "#VehicleId",* FROM #VehicleId 
--debug
SELECT 'UPDATE 1', AS "UPDATE 1", * FROM ResourceDaily 
     WHERE WorkDay = @scheduleDate 
       AND ResourceName = 'RUN 2' 
       AND VehicleId IN (SELECT VehicleId 
                           FROM #VehicleId)

    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)

--debug
SELECT 'UPDATE 2', AS "UPDATE 2", * FROM ResourceDaily 
     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

我添加了3个调试查询,它们将返回您需要查看并理解的结果集,然后才能解决此问题。我不知道您的应用程序或您正在尝试做什么,以便为您解决此问题,但您需要在解决问题之前查看您的查询正在做什么。如果最终导致#VehicleId在插入之后为空,我不会感到惊讶。如果是这样,请注意@scheduleDate参数的时间和WorkDay列值未正确连接的时间。查看Floor a date in SQL server