那就这样:
declare @news table(ts_cs varchar(100),ts_pm varchar(100))
declare @news_hq table(ts_cs varchar(100),ts_pm varchar(100))
insert @news
select 'a','aa-bb-cc' union all
select 'b','aa-bb-cc-dd' union all
select 'c','aa-bb-cc-ff-gg'
select top 100 id = identity(int,1,1) into #tmp from syscolumns
insert @news_hq(ts_cs,ts_pm)
select a.ts_cs,substring(a.ts_pm,b.id,1) as ts_pm
from @news a inner join #tmp b on substring('-' + a.ts_pm,b.id,1) = '-'
----查看
select * from @news_hq
这样试试:
declare @news table(ts_cs varchar(100),ts_pm varchar(100))
declare @news_hq table(ts_cs varchar(100),ts_pm varchar(100))
insert @news
select 'a','aa-bb-cc' union all
select 'b','aa-bb-cc-dd' union all
select 'c','aa-bb-cc-ff-gg'
select top 100 id = identity(int,1,1) into #tmp from syscolumns
insert @news_hq
select a.* from @news a inner join #tmp b on substring('-' + a.ts_pm,b.id,1) = '-'
----查看
select * from @news_hq
while exists (select * from @t)
begin
--插入前半部分
insert into news_hq (ts_cs,ts_pm) select ts_cs,left(ts_pm,charindex('-',ts_pm)-1) from @t where charindex('-',ts_pm)>0
update @t set ts_pm=stuff(ts_pm,1,charindex('-',ts_pm),'')
where charindex('-',ts_pm)>0
delete @t where ts_pm is null or ts_pm=''
insert into news_hq (ts_cs,ts_pm) select ts_cs,ts_pm from @t where charindex('-',ts_pm)<=0
while exists (select * from @t)
begin
--插入前半部分
insert into news_hq (ts_cs,ts_pm) select ts_cs,left(ts_pm,charindex('-',ts_pm)-1) from @t where charindex('-',ts_pm)>0
update @t set ts_pm=stuff(ts_pm,1,charindex('-',ts_pm),'')
where charindex('-',ts_pm)>0
delete @t where ts_pm is null or ts_pm=''
insert into news_hq (ts_cs,ts_pm) select ts_cs,ts_pm from @t where charindex('-',ts_pm)<=0