批量修改数据库内容

yyouyou 2008-12-01 09:38:59
有一张表A,有两列,t1和t2
id t2
aaa myid:111
bbb yourid:222
ccc herid:3333

现在想通过批量替换,变成
id t2
aaa myid:aaa
bbb yourid:bbb
ccc herid:ccc

请教各位如何更改?
...全文
58 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ken119 2008-12-13
  • 打赏
  • 举报
回复
我也学习一下啦
ljhcy99 2008-12-01
  • 打赏
  • 举报
回复


UPDATE TABLE
SET t2=LEFT(CHARINDEX(':',T2)-1,T2)+ID

yyouyou 2008-12-01
  • 打赏
  • 举报
回复
谢谢各位了O(∩_∩)O,马上给分
水族杰纶 2008-12-01
  • 打赏
  • 举报
回复
if object_id('tempdb..#')is not null drop table #
go
create table #(id varchar(10), t2 varchar(20))
insert # select 'aaa' , 'myid:111'
insert # select 'bbb' , 'yourid:222'
insert # select 'ccc' , 'herid:3333'
update # set t2=left(t2,charindex(':',t2))+id
select * from #
/*id t2
---------- --------------------
aaa myid:aaa
bbb yourid:bbb
ccc herid:ccc*/
hyde100 2008-12-01
  • 打赏
  • 举报
回复
declare @a table
(
id varchar(50),
t2 varchar(50)
)

insert into @a
select 'aaa' , 'myid:111' union
select 'bbb' , 'yourid:222'union
select 'ccc' , 'herid:3333'


update @a
set t2 = substring(t2,1,charindex(':',t2)) + id

select * from @a

结果:
aaa myid:aaa
bbb yourid:bbb
ccc herid:ccc
dawugui 2008-12-01
  • 打赏
  • 举报
回复
create table A(id varchar(10) ,   t2 varchar(20))
insert into a values('aaa' , 'myid:111')
insert into a values('bbb' , 'yourid:222')
insert into a values('ccc' , 'herid:3333')
go

update a
set t2 = left(t2 , charindex(':' , t2)) + id

select * from a

drop table a

/*
id t2
---------- --------------------
aaa myid:aaa
bbb yourid:bbb
ccc herid:ccc

(所影响的行数为 3 行)
/*
xieyueqing 2008-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hyde100 的回复:]
SQL codeupdate a
set t2 = substring(t2,1,charindex(':',t2)) + id
[/Quote]

就用这个吧
dawugui 2008-12-01
  • 打赏
  • 举报
回复
update a
set t2 = left(t2 , charindex(':')) + id
水族杰纶 2008-12-01
  • 打赏
  • 举报
回复
if object_id('tempdb..#')is not null drop table #
go
create table #(id varchar(10), t2 varchar(20))
insert # select 'aaa' , 'myid:111'
insert # select 'bbb' , 'yourid:222'
insert # select 'ccc' , 'herid:3333'
update # set t2=replace(replace(replace(t2,'111','aaa'),222,'bbb'),3333,'ccc')
select * from #
/*id t2
---------- --------------------
aaa myid:aaa
bbb yourid:bbb
ccc herid:ccc*/
hyde100 2008-12-01
  • 打赏
  • 举报
回复
update a
set t2 = substring(t2,1,charindex(':',t2)) + id

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧