把表中的某个字段格式如:2,3,4的数据分别插入到另一个表中

A表某个字段的格式是:"2,3,4,5..."
现在建了一个从表,要求把A表的这个字段的数据分别用单条插入从表中。
 
我们可以通过游标来实现:如

declare @temp_id int,@bb nvarchar(200)
declare temp_cur cursor for
select pkey,facility from Table_Hotel for update of pkey,facility
open temp_cur
fetch next from temp_cur into @temp_id,@bb
while @@fetch_status=0
begin
if @bb!=''
EXEC('insert into Table_HotelFac(HotelKey, FacValue, Price) select '+@temp_id+',value,0 from dbo.Table_Type where type=''HotelFacility'' and value in ('+@bb+')')
fetch next from temp_cur into @temp_id,@bb
end
deallocate temp_cur
原文地址:https://www.cnblogs.com/wzg0319/p/2137765.html