我在SQL Server 2008数据库中有一个具有datetime2字段的视图。我需要能够通过链接的SQL Server 2005从该视图进行查询。当我打开我的光标并获取记录时,我得到一个“从字符串转换日期时转换失败”。错误。如何使光标正确转换datetime2?
这是我的查询失败
DECLARE @time DateTime
DECLARE db_cursor CURSOR FOR
SELECT [DateTime] FROM [LINKEDSERVER].[DATABASENAME].[dbo].[ViewName]
WHERE Name = 'blah'
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @time
WHILE @@FETCH_STATUS = 0
BEGIN
Print @time
FETCH NEXT FROM db_cursor INTO @time
END
CLOSE db_cursor
DEALLOCATE db_cursor
有什么想法吗?
答案 0 :(得分:1)
尝试按以下方式声明游标:
DECLARE db_cursor CURSOR FOR
SELECT cast([DateTime] as DateTime) FROM [LINKEDSERVER].[DATABASENAME].[dbo].[ViewName]
WHERE Name = 'blah'
<强> BUT 强>
如果来自DATETIME2字段的远程值超出允许的DateTime类型,请尝试以下
DECLARE @time NVARCHAR(100)
DECLARE db_cursor CURSOR FOR
SELECT CAST([DateTime] as NVArCHAR(100)) FROM [LINKEDSERVER].[DATABASENAME].[dbo].[ViewName]
WHERE Name = 'blah'
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @time
WHILE @@FETCH_STATUS = 0
BEGIN
Print @time
FETCH NEXT FROM db_cursor INTO @time
END
CLOSE db_cursor
DEALLOCATE db_cursor