0

我正在尝试创建存储过程,它使用本文将特定时区城市的日期时间转换为另一个:在 SQL 2005 中有效地转换 UTC 和本地(即 PST)时间之间的日期

    ALTER PROCEDURE [dbo].[Get_TimeOfTimeZone]
    (
        @DateTimeFrom   datetime,
        @CityNameFrom   nvarchar(MAX),
        @CityNameTo     nvarchar(MAX),
        @ResultDate     datetime OUTPUT
    )

AS
    SET NOCOUNT ON

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    Declare @Query nvarchar(1000)
    Declare @ParmDefinition nvarchar (500)

    SET @Query = N''
    SET @Query = @Query + N'SELECT tz.Offset, tz.TimeZoneID '
    SET @Query = @Query + N'FROM TimeZoneCity, TimeZones tz WITH (NOLOCK) '
    --SET @Query = @Query + N'INNER JOIN    TimeZones WITH (NOLOCK) ON TimeZoneCity.TimeZoneID = tz.TimeZoneID '
    SET @Query = @Query + N'LEFT JOIN   TimeZoneDaylightSavings WITH (NOLOCK) ON TimeZoneDaylightSavings.TimeZoneID = tz.TimeZoneID '
    SET @Query = @Query + N'    WHERE TimeZoneCity.CityName = @CityNameTo '
    SET @Query = @Query + N'    AND TimeZoneCity.TimeZoneID = tz.TimeZoneID '
    SET @Query = @Query + N'        AND @DateTimeFrom >= TimeZoneDaylightSavings.BeginDst AND @DateTimeFrom < TimeZoneDaylightSavings.EndDst '

    SET @Query = @Query + N'SET @ResultDate =   DATEADD(HH, tz.Offset + CASE WHEN tz.TimeZoneID IS NOT NULL THEN 1 ELSE 0 END, '
    SET @Query = @Query + N'                            DATEADD(HH, (SELECT TimeZones.Offset '
    SET @Query = @Query + N'                                            FROM TimeZones, TimeZoneCity WITH (NOLOCK) ' 
    SET @Query = @Query + N'                                            WHERE TimeZoneCity.CityName = @CityNameFrom '
    SET @Query = @Query + N'                                            AND TimeZoneCity.TimeZoneID = TimeZones.TimeZoneID), '
    SET @Query = @Query + N'                                    @DateTimeFrom)) '

    SET @ParmDefinition = N'@ResultDate datetime, @DateTimeFrom datetime, @CityNameFrom nvarchar(MAX), @CityNameTo nvarchar(MAX) '

    EXECUTE sp_executesql @Query, @ParmDefinition, @ResultDate = @ResultDate, @DateTimeFrom = @DateTimeFrom, @CityNameFrom = @CityNameFrom, @CityNameTo = @CityNameTo

    SET NOCOUNT OFF 
RETURN

当我执行它时,我有这些错误:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "tz.Offset" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "tz.TimeZoneID" could not be bound.

任何人都可以检查我的存储过程并帮助我吗?非常感谢

4

1 回答 1

0

您在查询中混合JOIN类型。以下行使用逗号和WHERE子句连接

FROM TimeZoneCity, TimeZones tz WITH (NOLOCK) 

由于您正在混合连接语法,因此别名tz不可用。您应该始终使用相同的语法。这一行应该是:

SET @Query = @Query + N'FROM TimeZoneCity 
                        INNER JOIN TimeZones tz WITH (NOLOCK) 
                              ON TimeZoneCity.TimeZoneID = tz.TimeZoneID'
于 2013-03-19T16:32:45.180 回答