某种情况下,我们需要对数据库某个表的某些字段执行相同的修改,例如用户表存储的密码最初是以小写的形式进行存储的,如果我们需要将其全部转换为大写,以下t-sql语句可以解决此问题,原理是通过创建临时表来存储当前表的信息,再通过循环修改我们想要改变的密码字段:
/********************MC_User即为用户信息表************************/
declare @count int
declare @index int
declare @tmpID int
declare @tmpUserPassword nvarchar(50)
declare @tmpTable table
(
Inde int identity(1,1),
ID int,
UserPassword nvarchar(50)
)
insert into @tmpTable select a.ID,a.UserPassword from MC_User a
set @count=@@ROWCOUNT
set @index=1
while @index<=@count
begin
select @tmpID= ID,@tmpUserPassword= UserPassword from @tmpTable where Inde=@index
update MC_User set UserPassword=UPPER(@tmpUserPassword)
where ID=@tmpID
set @index=@index+1
end