光标示例中的Varchar到Int转换错误

时间:2014-06-27 12:23:12

标签: sql cursor

我第一次尝试游标但只获取varchar数据类型的错误。 错误是"转换varchar值时转换失败' SGS'数据类型int"

在我的表中,StationID是INT,而Station是VARCHAR。

我已编写此代码以显示我能够打印int,但是当我尝试使用varchar时,我无法打印。

DECLARE @vStationID INT, @vStation VARCHAR(50)
DECLARE MyCursor CURSOR
FOR 
SELECT StationID, Station FROM TblStation WHERE StationID='2'
OPEN MyCursor  
FETCH NEXT FROM MyCursor
INTO @vStationID,@vStation
PRINT @vStationID+' '+CONVERT(VARCHAR,@vStation)
CLOSE MyCursor
DEALLOCATE MyCursor

请告诉我代码中的错误是什么?

1 个答案:

答案 0 :(得分:1)

试试这个

DECLARE @vStationID INT, @vStation VARCHAR(50)
DECLARE MyCursor CURSOR
FOR 
SELECT StationID, Station FROM TblStation WHERE StationID='2'
OPEN MyCursor  
FETCH NEXT FROM MyCursor
INTO @vStationID,@vStation
PRINT convert(varchar,@vStationID)+' '+@vStation
CLOSE MyCursor
DEALLOCATE MyCursor