有一个TEST表
s_id s_aa s_ab
1 AA-AA-001 AB-AA-001
2 AA-AB-001 AB-AA-002
3 AA-AC-001 AB-AS-001
4 AA-AD-001 AB-AA-002
5 AB-AA-001 00-00-001
6 AB-AA-002 00-00-001
7 AA-AS-002 AB-AB-001
8 AB-AS-001 00-00-001想把s_ab字段里所有的00-00-001的s_aa字段为s_ab字段的数据修改为00-00-001,修改s_ab字段的数据时,s_aa字段里前2位要为'AA'.得到的结果如下:s_id s_aa s_ab
1 AA-AA-001 00-00-001
2 AA-AB-001 00-00-001
3 AA-AC-001 00-00-001
4 AA-AD-001 00-00-001
7 AA-AS-002 AB-AB-001
s_id s_aa s_ab
1 AA-AA-001 AB-AA-001
2 AA-AB-001 AB-AA-002
3 AA-AC-001 AB-AS-001
4 AA-AD-001 AB-AA-002
5 AB-AA-001 00-00-001
6 AB-AA-002 00-00-001
7 AA-AS-002 AB-AB-001
8 AB-AS-001 00-00-001想把s_ab字段里所有的00-00-001的s_aa字段为s_ab字段的数据修改为00-00-001,修改s_ab字段的数据时,s_aa字段里前2位要为'AA'.得到的结果如下:s_id s_aa s_ab
1 AA-AA-001 00-00-001
2 AA-AB-001 00-00-001
3 AA-AC-001 00-00-001
4 AA-AD-001 00-00-001
7 AA-AS-002 AB-AB-001
解决方案 »
- sql server 读取远程服务器上的CSV文件
- 在一个表里SELECT 希望检索出某两个字段相除为一固定值 的语句
- 问一sql语句
- DataGridView 列的显示不按顺序
- 报表执行时提示临时表已经存在,但是DROP时又没有?
- 关于排序的一个小问题
- A数据库的存储过程能将B数据库的数据存取过来吗?AB是局域网内不同的MSSQL数据库
- 帮忙,SQL语句求解!!
- 关于变量的问题,再次提出。
- 请问sql server 2000 中 select * from sysobjects where systat... 中 systat 的含义和用法
- 50分求一个SQL的通用存储过程,还带分页
- 我真的。生气了。索引就没用!
where left(s_aa,2)='AA' AND right(s_aa,3)='001'
set s_ab='00-00-01'
where left(s_aa,2)='AA'
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-10-21 22:18:06
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([s_id] int,[s_aa] varchar(9),[s_ab] varchar(9))
insert [tb]
select 1,'AA-AA-001','AB-AA-001' union all
select 2,'AA-AB-001','AB-AA-002' union all
select 3,'AA-AC-001','AB-AS-001' union all
select 4,'AA-AD-001','AB-AA-002' union all
select 5,'AB-AA-001','00-00-001' union all
select 6,'AB-AA-002','00-00-001' union all
select 7,'AA-AS-002','AB-AB-001' union all
select 8,'AB-AS-001','00-00-001'
--------------开始查询--------------------------
update
tb
set
s_ab='00-00-001'
where
left(s_aa,2)='aa' and right(s_aa,3)='001'select * from tb
----------------结果----------------------------
/* s_id s_aa s_ab
----------- --------- ---------
1 AA-AA-001 00-00-001
2 AA-AB-001 00-00-001
3 AA-AC-001 00-00-001
4 AA-AD-001 00-00-001
5 AB-AA-001 00-00-001
6 AB-AA-002 00-00-001
7 AA-AS-002 AB-AB-001
8 AB-AS-001 00-00-001(8 行受影响)*/
if object_id('test') is not null
drop table test
go
create table Test(s_id int,s_aa nvarchar(20),s_ab nvarchar(20))
goinsert into Test select
1,'AA-AA-001','AB-AA-001' union all select
2,'AA-AB-001','AB-AA-002' union all select
3,'AA-AC-001','AB-AS-001' union all select
4,'AA-AD-001','AB-AA-002' union all select
5,'AB-AA-001','00-00-001' union all select
6,'AB-AA-002','00-00-001' union all select
7,'AA-AS-002','AB-AB-001' union all select
8,'AB-AS-001','00-00-001'
goupdate Test Set s_ab='00-00-001' where left(s_aa,2)='AA' and right(s_aa,3)='001'
select * from test where s_aa like 'AA%'
/*s_id s_aa s_ab
----------- -------------------- --------------------
1 AA-AA-001 00-00-001
2 AA-AB-001 00-00-001
3 AA-AC-001 00-00-001
4 AA-AD-001 00-00-001
7 AA-AS-002 AB-AB-001(5 行受影响)
*/
-- Author: HEROWANG(让你望见影子的墙)
-- Date : 2009-10-21 22:29:03
---------------------------------
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
go
CREATE TABLE [tb] (s_id INT,s_aa VARCHAR(9),s_ab VARCHAR(9))
INSERT INTO [tb]
SELECT 1,'AA-AA-001','AB-AA-001' UNION ALL
SELECT 2,'AA-AB-001','AB-AA-002' UNION ALL
SELECT 3,'AA-AC-001','AB-AS-001' UNION ALL
SELECT 4,'AA-AD-001','AB-AA-002' UNION ALL
SELECT 5,'AB-AA-001','00-00-001' UNION ALL
SELECT 6,'AB-AA-002','00-00-001' UNION ALL
SELECT 7,'AA-AS-002','AB-AB-001' UNION ALL
SELECT 8,'AB-AS-001','00-00-001'select * from [tb]update s
set s.s_ab='00-00-001'
from tb s join tb t on s.s_ab=t.s_aa and t.s_ab='00-00-001'
where left(s.s_aa,2)='AA'1 AA-AA-001 00-00-001
2 AA-AB-001 00-00-001
3 AA-AC-001 00-00-001
4 AA-AD-001 00-00-001
5 AB-AA-001 00-00-001
6 AB-AA-002 00-00-001
7 AA-AS-002 AB-AB-001
8 AB-AS-001 00-00-001