一个处理 字符串分割 的 事务

create proc PrintStrArr
@Str nvarchar(2000),
@Split varchar(1)
as
declare @PosiInt int 
declare @StrCopy nvarchar(2000)
declare @StrItem nvarchar(200)
declare @ErrorNum int
set @ErrorNum=0
begin tran
	if LEN(@Str)>0
		begin
			set @StrCopy=@Str
			while len(@StrCopy)>0
				begin
					set @PosiInt= CHARINDEX(@Split, @StrCopy) 
					if @PosiInt>0
						begin
						   	set @StrItem=LEFT(@StrCopy,@PosiInt-1)
							set @StrCopy=RIGHT(@StrCopy,len(@StrCopy)- @PosiInt)
							Print @StrItem
							set @ErrorNum=@ErrorNum+@@ERROR
						end
					else
						begin
							set @StrItem=@StrCopy
							set @StrCopy=''
							print @StrItem
							set @ErrorNum=@ErrorNum+@@ERROR
						end
				end		
		end
	if (@ErrorNum<>0) 
		begin
		rollback tran
		return 0
        end  
	else
		begin
		commit tran
		return 1
        end 

 

posted @ 2015-07-22 18:59  bubadao  阅读(191)  评论(0编辑  收藏  举报