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