Concat所有参数并在Stored Proc中创建字符串?

时间:2011-03-11 02:10:38

标签: sql-server sql-server-2005 tsql

我在SP中有参数并尝试将它们作为字符串连接

@OBID int,
@ODID int,
@EMID int,
@TYPE varchar(20),
@LastModifiedBy int

SET @Strcat = @OBID +','+ @CursorODID +','+@EmpName +','+@TYPE

这是错误的。

 Error Description: Conversion failed when converting the varchar value ',' 
to data type int. 1 

2 个答案:

答案 0 :(得分:7)

将所有非字符串变量转换为字符串然后连接...

SET @Strcat = Convert(varchar(50), @OBID) +','+ Convert(varchar(50), @CursorODID) +','+@EmpName +','+@TYPE

答案 1 :(得分:0)

您需要先将其转换为nvarchar / varchar:

@OBID int,
@ODID int,
@EMID int,
@TYPE varchar(20),
@LastModifiedBy int

SET @Strcat = CONVERT(nvarchar,@OBID) +','+ CONVERT(nvarchar,@CursorODID) +','+@EmpName +','+@TYPE 

此致