mysql批量更新替换单引号_Mssql中替换单引号和返回更新记录的写法

以下为引用的内容:

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,1,charindex(@c,@str)-1)

set @n=@n+@@rowcount       --每执行一次保存更新行数到这个变量

exec sp_executesql @sql

set @n=@n+@@rowcount

set @str=stuff(@str,1,charindex(@c,@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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值