select replace(字段B,'王','A') into #temp from 表F update 表F a set a.字段B=b.字段B from 表F a ,#temp b
declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6)) insert @t select '王1','王' union all select '王2','王' union all select '王3','王' union all select '王N','王'update @t set [c1]=replace([c1],'王','A') update @t set [c2]='w'+ltrim(id)select * from @t /* id c1 c2 ----------- ---- ------ 1 A1 w1 2 A2 w2 3 A3 w3 4 AN w4 */--id不连续的话用row_number() 生成连续id号即可。
--不好意思,上面有误 IF exists (select 1 from sysobjects where name='[F]'and type='p') drop table [F] go create table F (B varchar(4)) insert [F] select '王1'union all select '王2'union all select '王3'union all select '王4'union all select '王5'-- select * from F --1 update F set B=replace(B,'A','王') --2 select B=replace(B,'王','A') into #temp from F update F set B=b.B from F,#temp b/* B 王1 王2 王3 王4 王5 (所影响的行数为 5 行) */
declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6)) insert @t select '王1','王' union all select '王2','王' union all select '王3','王' union all select '王N','王' UPDATE @t SET [c1] = REPLACE([c1],'王','A'), [c2] = REPLACE([c2],'王','W'+LTRIM(id))
----------------------------------------------------------------- /** * 测试 3 楼 报错,也许是我理解错了,望指点 * 测试row_number()函数 * 2012-8-14 * **/ ----------------------------------------------------------------- declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6)) insert @t select '王1','王' union all select '王2','王' union all select '王3','王' union all select '王N','王'update @t set [c1]=replace([c1],'王','A') update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t------------------------------------------------------报错: 消息 4108,级别 15,状态 1,第 9 行 开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
----------------------------------------------------------------- /** * 测试 3楼 ,也许我理解错了,望指点 * 测试row_number()函数 * 2012-8-14 * **/ ----------------------------------------------------------------- declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6)) insert @t select '王1','王' union all select '王2','王' union all select '王3','王' union all select '王N','王'update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t报错: 消息 4108,级别 15,状态 1,第 9 行 开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
----------------------------------------------------------------- /** * 测试 3 楼 ,也许理解错了,没出来,望指点 * 测试row_number()函数 * 2012-8-14 * **/ ----------------------------------------------------------------- declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6)) insert @t select '王1','王' union all select '王2','王' union all select '王3','王' union all select '王N','王' update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t报错:消息 4108,级别 15,状态 1,第 9 行 开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
--假设ID不连续 declare @t table(id int,[c1] varchar(4),[c2] varchar(6)) insert @t select 2,'王1','王' union all select 4,'王2','王' union all select 7,'王3','王' union all select 10,'王N','王'update @t set [c1]=replace([c1],'王','A') update @t set [c2]='W'+ltrim(b.rid) from @t a left join (select id,row_number() over (order by id) as rid from @t) b on a.id=b.idselect * from @t /* id c1 c2 ----------- ---- ------ 2 A1 W1 4 A2 W2 7 A3 W3 10 AN W4 */
set b = replace(b,'王','A')
select replace(字段B,'王','A') into #temp from 表F update 表F a set a.字段B=b.字段B from 表F a ,#temp b
declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
insert @t
select '王1','王' union all
select '王2','王' union all
select '王3','王' union all
select '王N','王'update @t set [c1]=replace([c1],'王','A')
update @t set [c2]='w'+ltrim(id)select * from @t
/*
id c1 c2
----------- ---- ------
1 A1 w1
2 A2 w2
3 A3 w3
4 AN w4
*/--id不连续的话用row_number() 生成连续id号即可。
--不好意思,上面有误
IF exists (select 1 from sysobjects where name='[F]'and type='p')
drop table [F]
go
create table F (B varchar(4))
insert [F]
select '王1'union all
select '王2'union all
select '王3'union all
select '王4'union all
select '王5'-- select * from F
--1
update F set B=replace(B,'A','王')
--2
select B=replace(B,'王','A') into #temp from F update F set B=b.B from F,#temp b/*
B
王1
王2
王3
王4
王5
(所影响的行数为 5 行)
*/
insert @t
select '王1','王' union all
select '王2','王' union all
select '王3','王' union all
select '王N','王'
UPDATE @t
SET [c1] = REPLACE([c1],'王','A'),
[c2] = REPLACE([c2],'王','W'+LTRIM(id))
/**
* 测试 3 楼 报错,也许是我理解错了,望指点
* 测试row_number()函数
* 2012-8-14
*
**/
-----------------------------------------------------------------
declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
insert @t
select '王1','王' union all
select '王2','王' union all
select '王3','王' union all
select '王N','王'update @t set [c1]=replace([c1],'王','A')
update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t------------------------------------------------------报错:
消息 4108,级别 15,状态 1,第 9 行
开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
/**
* 测试 3楼 ,也许我理解错了,望指点
* 测试row_number()函数
* 2012-8-14
*
**/
-----------------------------------------------------------------
declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
insert @t
select '王1','王' union all
select '王2','王' union all
select '王3','王' union all
select '王N','王'update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t报错:
消息 4108,级别 15,状态 1,第 9 行
开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
/**
* 测试 3 楼 ,也许理解错了,没出来,望指点
* 测试row_number()函数
* 2012-8-14
*
**/
-----------------------------------------------------------------
declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
insert @t
select '王1','王' union all
select '王2','王' union all
select '王3','王' union all
select '王N','王'
update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t报错:消息 4108,级别 15,状态 1,第 9 行
开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
--假设ID不连续
declare @t table(id int,[c1] varchar(4),[c2] varchar(6))
insert @t
select 2,'王1','王' union all
select 4,'王2','王' union all
select 7,'王3','王' union all
select 10,'王N','王'update @t set [c1]=replace([c1],'王','A')
update @t set [c2]='W'+ltrim(b.rid)
from @t a left join (select id,row_number() over (order by id) as rid from @t) b
on a.id=b.idselect * from @t
/*
id c1 c2
----------- ---- ------
2 A1 W1
4 A2 W2
7 A3 W3
10 AN W4
*/
比如 表 table
name phone
1 313464614
2 35464645
3 2464674
4 25465464
5 4514964把phone的第一位全部替换成9
求SQL