日期:2014-05-18  浏览次数:20347 次

求助!SQL写法问题,把筛选的结果一次性修改
在TF_BOM表, PRD_NO这个字段:

字段 PRD_NO
---------------------
  abc  
  abcR
  cde  
  cdeR
  def  
  defR
---------------------

下面语句是我筛选出结果的SQL:
----------------------------------
select distinct PRD_NO from TF_BOM where PRD_NO in(select left(PRD_NO,len(PRD_NO) -1) from TF_BOM where right(PRD_NO,1) ='R')

我想把结果一次性修改成带R的,求教怎么写SQL,可以用多行代码(只要能在查询器里执行就可以)

------解决方案--------------------
SQL code

--> 测试数据:[test]
if object_id('[test]') is not null 
drop table [test]
create table [test]([PRD_NO] varchar(4))
insert [test]
select 'abc' union all
select 'abcR' union all
select 'cde' union all
select 'cdeR' union all
select 'tyt' union all
select 'tytR' union all
select 'gfh' union all
select 'fgg' union all
select 'def' union all
select 'defR'


update test  
set [PRD_NO]=[PRD_NO]+'R' 
where exists(select 1 from test b where test.PRD_NO=left(b.PRD_NO,LEN(b.PRD_NO)-1))

select * from test
/*
PRD_NO
-----------
abcR
abcR
cdeR
cdeR
tytR
tytR
gfh
fgg
defR
defR
*/

--你看看这个结果对不?