例如
yfshop 里边有表 WFOrg WFPosition WFOrg 表包含字段1 OrgID √ nvarchar 100 50 0
2 OrgName √ nvarchar 200 100 0 √
3 ParentOrgID varchar 50 50 0 √
4 IsRoot smallint 2 5 0 √ (1)
5 IsFinish smallint 2 5 0 √ (1)
6 Re nvarchar 510 255 0 √ WFPosition 1 PositionID √ nvarchar 100 50 0
2 UserID √ nvarchar 100 50 0 √
3 OrgID nvarchar 100 50 0 √
4 RoleID nvarchar 100 50 0 √
5 DutyID nvarchar 100 50 0 √
6 IsFinish smallint 2 5 0 √ (1)
7 Re nvarchar 510 255 0 √ 现在
WFOrg 表里边 字段IsFinish Re 包含username
WFPosition 表里 字段 IsFinish Re 包含 username
现在要把 所有username 转换成xingming
yfshop 里边有表 WFOrg WFPosition WFOrg 表包含字段1 OrgID √ nvarchar 100 50 0
2 OrgName √ nvarchar 200 100 0 √
3 ParentOrgID varchar 50 50 0 √
4 IsRoot smallint 2 5 0 √ (1)
5 IsFinish smallint 2 5 0 √ (1)
6 Re nvarchar 510 255 0 √ WFPosition 1 PositionID √ nvarchar 100 50 0
2 UserID √ nvarchar 100 50 0 √
3 OrgID nvarchar 100 50 0 √
4 RoleID nvarchar 100 50 0 √
5 DutyID nvarchar 100 50 0 √
6 IsFinish smallint 2 5 0 √ (1)
7 Re nvarchar 510 255 0 √ 现在
WFOrg 表里边 字段IsFinish Re 包含username
WFPosition 表里 字段 IsFinish Re 包含 username
现在要把 所有username 转换成xingming
select name from syscolumns where id=object_id('表名'))
select name from syscolumns where id=object_id('表名'))
update WFOrg set IsFinish=replace(IsFinish,'usernam','xingming'),Re=replace(Re,'usernam','xingming')update WFPosition set IsFinish=replace(IsFinish,'usernam','xingming'),Re=replace(Re,'usernam','xingming')
select replace(name,'username','xingming') where name in(
select name from syscolumns where id=object_id('表名'))
这sql 明显有错吗?
语法错误有where却没有表名
感谢你的回贴
update WFOrg set IsFinish=replace(IsFinish,'usernam','xingming'),Re=replace(Re,'usernam','xingming')update WFPosition set IsFinish=replace(IsFinish,'usernam','xingming'),Re=replace(Re,'usernam','xingming')
你是对特定的表特定的字段进行操作 我现在表很多 而且表里边的字段也很多 所有我没有办法来对特定的表特定的字段进行操作
--那么用动态SQL可以实现批量更新
declare @str varchar(8000)
set @str=''
select @str=@str+'update '+a.name+' set '+b.name+'=replace('+b.name+',''usernam'',''xingming'')'+CHAR(10)
from sysobjects a,syscolumns b, systypes c where a.id=b.id and a.xtype='U'and b.xtype=c.xtype and c.name='nvarchar'
exec(@str)
go