第二步是将翻译好的关键字导入到数据库中,并更新ImageData表--翻译好的escel路径 F:\datamanage\upexcel\translate.xlsif exists(select * from tempdb.dbo.sysobjects where id=object_id('tempdb.dbo.#xy200606072'))
drop table #xy200606072
create table #xy200606072(keyword varchar(100),chinese varchar(100),plurality varchar(100),past varchar(100)) 
-- 导入excel数据到零时表
insert into #xy200606072 (keyword,chinese,plurality,past) select keyword,chinese,plurality,past  from 
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 8.0;HDR=Yes;Database=F:\datamanage\upexcel\translate.xls',trans$) where keyword  is not null
-- 删除表Translate中已有的数据
delete from #xy200606072
where keyword in (select keyword from Translate)
-- 导入到表Translate
insert into Translate(keyword,chinese,plurality,past) select keyword,chinese,plurality,past  from #xy200606072
-- 再去更新ImageData表,将新翻译的中文关键字对应更新到ImageData
declare @N1 int,@INO varchar(100),@KW1 varchar(4000),@KW2 varchar(100),@KW3 varchar(4000),@COUNT INT
select @count=count(1) from ImageData
while @count>0
begin
set @KW3=N''
if isnull(@INO,'')=''
select top 1 @INO=Imageno,@KW1=keywords from ImageData order by Imageno
else
select top 1 @INO=Imageno,@KW1=keywords from ImageData where Imageno>@INO order by Imageno 
while len(@KW1)-len(replace(@KW1,',',''))>0
begin
select @N1=charindex(',',@KW1)
select @KW2=left(@KW1,@N1-1),@KW1=substring(@KW1,@N1+1,LEN(@KW1)-@N1)
select @KW3=ISNULL(@KW3+[chinese]+',','') from #xy200606072 where keyword=@KW2 or [plurality]=@KW2 or [past]=@KW2
end
update ImageData set KeywordsinCHN=KeywordsinCHN+@KW3 where Imageno=@INO
set @count=@count-1
end
drop table #xy200606072

解决方案 »

  1.   

    给你个例子看看create table aa(id int,keywords nvarchar(4000),chnwords nvarchar(4000))insert into aa select 1,'aa,bb,cc,dd',null
    insert into aa select 2,'',null
    insert into aa select 3,'aa',null
    --drop table bb
    create table bb(id int,keyword nvarchar(50),chn nvarchar(100))
    insert into bb select 1,'aa','文学'
    insert into bb select 2,'bb','语文'
    --处理没有多个关键字的情况
    update aa set keywords='',chnwords=bb.chn from aa
    left join bb on aa.keywords=bb.keyword
    where patindex('%,%',keywords)=0--处理第一个','
    update aa set keywords=substring(aa.keywords,patindex('%,%',keywords)+1,len(aa.keywords)),chnwords=isnull(chnwords,N'')+case when bb.chn is null then '' else bb.chn+',' end from aa
    left join bb on  substring(aa.keywords,1,patindex('%,%',keywords)-1)=bb.keyword
    where patindex('%,%',keywords)>0while @@rowcount >0
    begin
       update aa set keywords=substring(aa.keywords,patindex('%,%',keywords)+1,len(aa.keywords)),chnwords=isnull(chnwords,N'')+case when bb.chn is null then '' else bb.chn+',' end from aa
    left join bb on  substring(aa.keywords,1,patindex('%,%',keywords)-1)=bb.keyword
     where patindex('%,%',keywords)>0
    end--处理最后一个关键字
    update aa set keywords='',chnwords=isnull(chnwords,N'')+isnull(bb.chn,'') from aa
    left join bb on aa.keywords=bb.keyword
    where isnull(keywords,'')<>''--删除最后一个chnwords中的','
    update aa set chnwords=substring(chnwords,1,len(chnwords)-1)
     where patindex('%,%',chnwords)>0select * from aa