Create proc Attention
@id int,--用户ID
@uid varchar(2000),--需要操作的用户ID
@type int--操作类型 1:取消关注 2:关注
as
begin
declare @data varchar(2000),--数据源
@split varchar(2),--分割符
@val varchar(2000),--储存最终需要修改的字符串
@t varchar(2000),--存放已经添加过的数据
@txt varchar(2000),--存储一个临时字符串
@num varchar(2)--代码控制,第一次执行不需要截取顿号右边的值,不然会报错
set @split='、'--分割符
set @num='0'
select @data=Name from StudentInfoes where StudentId=@id--数据源 如1、2、3、4
if @type=1--取消关注
begin
while(charindex(@split,@data)<>0)
begin
set @t=CONCAT(@val,substring(@data,1,charindex(@split,@data)-1))--取字符串
set @txt=CHARINDEX('、',@t)
if @num!='0' and @txt!='0' --判断是否为第一次加载,判断顿号右边是否有数据。否则会报错
begin
set @txt=reverse(substring(reverse(@t),1,charindex('、',reverse(@t)) - 1)) --取特殊符号右边的字符串
end
set @num='1'
if (@t!=@uid and @txt is null) or (@t!=@uid and @txt!=@uid)
begin
set @val=CONCAT(@t,'、')--组装最终数据
end
set @data = stuff(@data,1,charindex(@split,@data),'') --将取出来的字符串删除掉
end
if @data!=@uid
begin
set @val=CONCAT(@val,@data)--添加最后一个字符串
end
set @txt=right(@val,1)--取最后一位字符串
if @txt='、'--如果最后一个字符为特殊符号
begin
set @val=left(@val,len(@val)-1)--那么删除最后一个特殊符号
end
end
if @type=2--关注
begin
set @val=CONCAT(@data,'、'+@uid)--组装关注数据
end
update StudentInfoes set Name=@val where StudentId=@id--进行Update修改
end