消息 245,级别 16,状态 1,第 1 行
在将 varchar 值 '2,7' 转换成数据类型 int 时失败。
原sql
select UserName from s_User where Id in ( select UndertakerList from ZW_HandlerSend where HsID=7 )
改成
select UserName from s_User where charindex(rtrim(Id), (select top 1 UndertakerList from ZW_HandlerSend where HsID=7 ))>0
第2种写法
select UserName from s_User
where charindex(','+convert(varchar,id)+',',','+'2,7'+',')>0
参考:
http://blog.csdn.net/hongsejiaozhu/article/details/4978737
--------------------------
sql将多行数据转成一行数据
TabB ( id int, name varchar(32)) 数据2条
现在,我想查出一行一列数据
第1种写法:
第2种写法:
create function [dbo].[fn_getHSUndertakerList222](@HsID int)
returns varchar(1024)
as
begin
declare @buildSQL nvarchar(1024)
declare @str varchar(1024)
set @str=''
select @str = @str + ',' + UserName from s_User
where ( charindex(rtrim(Id), (select top 1 UndertakerList from ZW_HandlerSend where HsID=@HsID ))>0 )
set @str = substring(@str,2,len(@str))
return @str end
select dbo.[fn_getHSUndertakerList222](7)