我正在尝试创建存储过程,使用本文将特定时区城市的日期时间转换为另一个:Effectively Converting dates between UTC and Local (ie. PST) time in SQL 2005
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.
有人可以查看我的存储过程并帮助我吗?非常感谢
答案 0 :(得分: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'