2011-07-30 54 views
0

我已經逗號分隔字符串TSQL打破了逗號分隔字符串,然後循環

a=1,2,3,4 

現在我想通過它在TSQL在SQL中使用1,2,3等查詢打破這個字符串,然後循環Server 2008的

set @sql = @sql + ' and (ClassicStation.int_WheatherTypeId = a[i]) AND (ClassicStation.int_MeasurementId IN (1,2)) or' 

回答

0

在您的例子,它看起來像你對我可以做這個:

set @sql = @sql + 'and (ClassicStation.int_WheatherTypeId in ('[email protected]+')) 
AND (ClassicStation.int_MeasurementId IN (1,2))' 

否則,你可以拆分與位於任何地方或 這樣

declare @a varchar(max) 
set @a ='1,2,3,4' + ',' 

;with csv (col, pos) as 
(
select left(@a, charindex(',', @a) -1),charindex(',', @a) 
union all 
select substring(@a, pos +1, charindex(',', @a, pos +1) - pos-1), 
charindex(',',@a, pos+1) from csv 
where pos < len(@a) 
) 
select * from csv 
分割函數的字符串