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

求2条SQL语句,关于批量修改和批量删除的
Book表   字段   BarCode(条形码).
比如有几条记录是这样的   以第2条为标准!
BookName             BarCode                         BookInfo                         BookPrice
砍死赵微           11008200956                   想砍就砍                               25
爱死小姿           1100820011957               想爱就爱                               65
锤死赵微           11008200958                   想锤就锤                               25
亲亲小姿           11008200959                   想亲就亲                               55    
抱抱小姿           110082001122960           想抱就抱                               45
吻吻小姿           110082001122961           想吻就吻                               75
现在想实现如下功能:
我入库后发现有的记录条形码少输入了2位,而有多多输了2位
写一个批量修改的SQL,把少了2位的条形码补齐(参照第2条,少了2个11)
写一个批量修改的SQL,把多了2位的条形码那2多余的删掉(参照第2条,多了2个22)
写一个批量删除的SQL,把其他不整齐的条形码的入库记录删掉


------解决方案--------------------
create table T(BookName varchar(100),BarCode varchar(100),BookInfo varchar(100),BookPrice int)

insert into T select '砍死赵微 ', '11008200956 ', '想砍就砍 ',25
insert into T select '爱死小姿 ', '1100820011957 ', '想爱就爱 ',65
insert into T select '锤死赵微 ', '11008200958 ', '想锤就锤 ',25
insert into T select '亲亲小姿 ', '11008200959 ', '想亲就亲 ',55
insert into T select '抱抱小姿 ', '110082001122960 ', '想抱就抱 ',45
insert into T select '吻吻小姿 ', '110082001122961 ', '想吻就吻 ',75

--将少2位的更新
update T set BarCode = left(BarCode,8) + '11 ' + right(BarCode,3) where len(BarCode)=11

--将多2位的更新
update T set BarCode = left(BarCode,8) + '11 ' + right(BarCode,3)where len(BarCode)=15

--更新后,将长度不等于13的删除
delete T where len(BarCode) <> 13
select * from T

drop table T
------解决方案--------------------
有其他写法吗?比如subString()..

还有,怎么给你分?

另请教其他写法
------解决方案--------------------
declare @T table(BookName varchar(100),BarCode varchar(100),BookInfo varchar(100),BookPrice int)

insert into @T select '砍死赵微 ', '11008200956 ', '想砍就砍 ',25
insert into @T select '爱死小姿 ', '1100820011957 ', '想爱就爱 ',65
insert into @T select '锤死赵微 ', '11008200958 ', '想锤就锤 ',25
insert into @T select '亲亲小姿 ', '11008200959 ', '想亲就亲 ',55
insert into @T select '抱抱小姿 ', '110082001122960 ', '想抱就抱 ',45
insert into @T sele