转换varchar值时转换失败','到SQL Server中的数据类型int

时间:2016-10-17 12:54:39

标签: sql-server tsql sql-server-2012

这些行中出现错误:

Select
    @RankId = @RankId + ', ' + ID 
From
    RANKS 
Where
    RANKS.ID IN (Select RANK_ID 
                 From WORKFLOW_RANK 
                 Where WORKFLOW_ID = @WFID)

Select 
    @LocationId = @LocationId + ', ' + ID 
From 
    LOCATIONS 
Where 
    LOCATIONS.ID in (Select LOCATION_ID 
                     From WORKFLOW_LOCATION 
                     Where WORKFLOW_ID = @WFID)

RankIdLocationId在表格中声明为int

完整的代码是:

Declare @i As Int
    ,@mx    As Int
    ,@Ranks As nVarchar(250)
    ,@Loc   As nVarchar(250)
    ,@WF    As nVarchar(250)
    ,@WFID as int
    ,@RankId as int
    ,@LocationId as int

Create Table #Result (ID Int Identity(1,1),WORKFLOW_ID  int,WORKFLOW nVarchar(100), RANKS Varchar(250), LOCATIONS Varchar(250),RankId int,LocationId int)

Insert Into #Result (WORKFLOW_ID, WORKFLOW) 
    Select Distinct ID, WORKFLOW_NAME 
    From [WORKFLOW] 
    Where ID = 1

Select @i = 1, @mx = Max(ID) 
From #Result

While (@i < = @Mx)
Begin
    Select 
        @WFID = WORKFLOW_ID, 
        @WF = WorkFlow, 
        @Ranks = '',
        @Loc = ''  
    From 
        #Result 
    Where 
        ID = @i

    Select 
        @Ranks = @Ranks + ', ' + RANK_NAME 
    From 
        RANKS 
    Where 
        RANKS.ID in (Select RANK_ID From WORKFLOW_RANK 
                     Where WORKFLOW_ID = @WFID)

    Select 
        @Loc = @Loc + ', ' + LOCATION_NAME 
    From 
        LOCATIONS 
    Where 
        LOCATIONS.ID in (Select LOCATION_ID From WORKFLOW_LOCATION 
                         Where WORKFLOW_ID = @WFID)

    Select 
        @RankId = @RankId + ', ' + ID 
    From 
        RANKS 
    Where 
        RANKS.ID in (Select RANK_ID From WORKFLOW_RANK 
                     Where WORKFLOW_ID = @WFID)

    Select 
        @LocationId = @LocationId + ', ' + ID 
    From 
        LOCATIONS 
    Where 
        LOCATIONS.ID in (Select LOCATION_ID From WORKFLOW_LOCATION 
                         Where WORKFLOW_ID = @WFID)

    Update #Result 
    Set RANKS = Right(@Ranks, Len(@Ranks) - 2), 
        LOCATIONS = Right(@Loc, Len(@Loc) - 2),
        RankId = Right(@RankId, Len(@RankId) - 2),
        LocationId = Right(@LocationId, Len(@LocationId) - 2)
    Where ID = @i

    SET @i = @i + 1
End

Select * From #Result
Drop Table #Result

2 个答案:

答案 0 :(得分:1)

在SQL中,您无法显式地将字符串字符组合或添加到整数值。使用CASTCONVERT功能来实现此目的。

尝试使用以下查询。由于您将字符串更改为变量@RankId@LocationId

的数据类型
DECLARE @RankId VARCHAR(8000)
DECLARE @LocationId  VARCHAR(8000)

第一次查询:

  Select @RankId = COALESCE(@RankId + ',', '') + CAST(ID AS VARCHAR(50)) 
  From RANKS 
  Where RANKS.ID in (Select RANK_ID From WORKFLOW_RANK Where WORKFLOW_ID = @WFID)

第二名:

  Select @LocationId = COALESCE(@LocationId + ',', '') +  CAST(ID AS VARCHAR(50)) 
  From LOCATIONS 
  Where LOCATIONS.ID in (Select LOCATION_ID From WORKFLOW_LOCATION Where WORKFLOW_ID = @WFID)

完整代码应更改如下:

Declare @i As Int
    ,@mx    As Int
    ,@Ranks As nVarchar(250)
    ,@Loc   As nVarchar(250)
    ,@WF    As nVarchar(250)
    ,@WFID as int
    ,@RankId as Varchar(250)
    ,@LocationId as Varchar(250)

Create Table #Result (ID Int Identity(1,1),WORKFLOW_ID  int,WORKFLOW nVarchar(100), RANKS Varchar(250), LOCATIONS Varchar(250),RankId VARCHAR(250),LocationId VARCHAR(250))

Insert Into #Result (WORKFLOW_ID,WORKFLOW) Select Distinct ID,WORKFLOW_NAME From [WORKFLOW] where ID=1

Select @i = 1, @mx = Max(ID) From #Result

WHILE (@i < = @Mx)
    Begin
        Select @WFID=WORKFLOW_ID, @WF = WorkFlow, @Ranks = '',@Loc =''  From #Result Where ID = @i

        Select @Ranks = @Ranks + ', ' + RANK_NAME From RANKS Where RANKS.ID in (Select RANK_ID From WORKFLOW_RANK Where WORKFLOW_ID = @WFID)

        Select @Loc = @Loc + ', ' + LOCATION_NAME From LOCATIONS Where LOCATIONS.ID in (Select LOCATION_ID From WORKFLOW_LOCATION Where WORKFLOW_ID = @WFID)

       Select @RankId = COALESCE(@RankId + ',', '') + CAST(ID AS VARCHAR(50)) 
       From RANKS 
       Where RANKS.ID in (Select RANK_ID From WORKFLOW_RANK Where WORKFLOW_ID = @WFID)

       Select @LocationId = COALESCE(@LocationId + ',', '') +  CAST(ID AS VARCHAR(50)) 
       From LOCATIONS 
       Where LOCATIONS.ID in (Select LOCATION_ID From WORKFLOW_LOCATION Where WORKFLOW_ID = @WFID)

        Update #Result Set RANKS =Right(@Ranks,Len(@Ranks)-2), 

        LOCATIONS = Right(@Loc,Len(@Loc)-2),

        RankId = Right(@RankId,Len(@RankId)-2),

        LocationId = Right(@LocationId,Len(@LocationId)-2)

        Where ID = @i

        SET @i = @i + 1
    End

Select * From #Result
Drop Table #Result

答案 1 :(得分:0)

您尝试附加varcharint数据类型时收到错误。如果您使用的是 SQL Server 2008或更低版本,则必须将所有内容转换为varchar,然后附加它。

DECLARE @RankId VARCHAR(1000)

Select @RankId = COVERT(VARCHAR(50),@RankId) + ', ' + CONVERT(VARCHAR(50),ID) 
From RANKS Where RANKS.ID 
in (Select RANK_ID From WORKFLOW_RANK Where WORKFLOW_ID = @WFID)

如果您使用 SQL Server 2012或更高版本,那么您有一个函数CONCAT

DECLARE @RankId VARCHAR(1000)

Select @RankId = CONCAT(@RankId,',',ID) 
From RANKS Where RANKS.ID 
in (Select RANK_ID From WORKFLOW_RANK Where WORKFLOW_ID = @WFID)

DECLARE @LocationId VARCHAR(1000)

Select @LocationId = CONCAT(@LocationId, ', ', ID)
From LOCATIONS Where LOCATIONS.ID 
in (Select LOCATION_ID From WORKFLOW_LOCATION Where WORKFLOW_ID = @WFID)

更新:您需要将@RankId和@LocationId声明为varchar(1000)