以下为引用的内容:
declare @str varchar(500) --定义从分割字符串的字符变量
declare @istr varchar(500) --定义从分割字符串的比较变量
declare @insql varchar(500) --定义从分割字符串从新组成SQL字符串数组变量
declare @sql nvarchar(500) --定义SQL中执行语句变量
declare @no varchar(20) --定义分店中引用总店的编号变量(没什么用处)
declare @c varchar(2) --定义以什么字符进行分割的变量
declare @n int --定义记录更新数据的行数变量
set @n=0 --初试化更新为0
set @str='3035,3056' --可以在程序中写入内容
set @istr='3035,3056' --同上
set @c=',' --同上
begin
if (charindex(@c,@str)=0) --如果要分割的字符在字符串中不存在
begin
select @insql=vList,@no=vNo from bProduction where [id]=@istr --从表中查找字段并附值给变量
select @insql=''''+replace(@insql,',''',''')+'''' --替换单引号
select @sql='update bProduction set vIpNo=''00010014'' where vMemberId in('+@insql+') and vNo='''+@no+''''
exec sp_executesql @sql --执行字符串变量的SQL
set @n=@n+@@rowcount --每执行一次保存更新行数到这个变量
end
while(charindex(@c,@str)<>0) --如果要分割的字符在字符串中存在就循环执行
begin
select @insql=vList,@no=vNo from bProduction where [id]=substring(@str,1,charindex(@c,@str)-1)
select @insql=''''+replace(@insql,''')+''''
select @sql='update bProduction set vIpNo=''00010014'' where vMemberId in('+@insql+') and vNo='''+@no+''''
update bProduction set vIpNo='00010014' where [id]=substring(@str,@str)-1)
set @n=@n+@@rowcount --每执行一次保存更新行数到这个变量
exec sp_executesql @sql
set @n=@n+@@rowcount
set @str=stuff(@str,@str),'') --分割字符串中的最后一个字段(需要依次记录下来)
end
if (@str<>@istr) --如果字符串数组是单个字符串
begin
select @insql=vList,@no=vNo from bProduction where [id]=@str
select @insql=''''+replace(@insql,''')+''''
select @sql='update bProduction set vIpNo=''00010014'' where vMemberId in('+@insql+') and vNo='''+@no+''''
exec sp_executesql @sql
set @n=@n+@@rowcount
update bProduction set vIpNo='00010014' where [id]=@str
set @n=@n+@@rowcount
end
else
begin
update bProduction set vIpNo='00010014' where [id]=@str
set @n=@n+@@rowcount
end
end
select @n as ncount --查询更新的记录
--@@rowcount是系统变量,每执行一次sql语句,@@rowcount中就存放了影响的函数
--table:bProduction column: id,vList,vNo,vIpNo,vMemberId
author: dreamman_yx