以下為引用的內容: 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 |