SQL Server數(shù)組參數(shù)的執(zhí)行與問題
以下的文章主要描述的是SQL Server數(shù)組參數(shù),以及對其實(shí)際操作的過程時(shí)碰到的相關(guān)問題的具體描述,在實(shí)際操作中我曾遇到過這樣的一個(gè)問題,需要在庫里處理N條數(shù)據(jù),不想在程序里循環(huán)造成多次訪問數(shù)據(jù)庫。
而SQL Sever又不支持SQL Server數(shù)組參數(shù),嘗試了兩種方法,在此做筆記如下(省略了單條數(shù)據(jù)的處理,用#temp是為了顯示結(jié)果)
1.利用replace create table #temp
- (
- ss varchar(200) not null
- )
- declare @str varchar(200)
- declare @result varchar(1000)
- set @str='aaa,bb,c,d,e,ffffff'
- set @result =' insert into #temp(ss) select '''+replace(@str,',','''union select''')+''''
- exec(@result)
- select * from #temp
2.利用charindex和substring
- create table #temp
- (
- ss varchar(200) not null
- )
- declare @str varchar(200)
- declare @curr int
- declare @prev int
- set @str='aaa,bb,c,d,e,ffffff'
- set @curr=1
- set @prev=1
- while @prev < len(@str)
- begin
- set @curr=charindex(',',@str,@prev)
- if @curr>@prev
- insert #temp select substring(@str,@prev,@curr-@prev)
- else
- begin
- insert #temp select substring(@str,@prev,len(@str)-@prev+1)
- break
- end
- set @prev=@curr+1
- end
- select * from #temp
以上的相關(guān)內(nèi)容就是對SQL Server數(shù)組參數(shù)的介紹,望你能有所收獲。
【編輯推薦】