table of Contents
Preface
Since the following code block has been used before, the risk is relatively high. The data in a table was messed up at once, and the database had to be restored and replaced again, possibly because the where condition was not written.
update A SET A.cj = b.cj FROM A ,B WHERE A.Name = B.Name
Then I wrote the following code based on my usual accumulation, which I can verify, and the risk is relatively small.
first step:
First query the statement you want to a temporary table. You can check whether the insertion is successful
select ROW_NUMBER() OVER(ORDER BY ID)as RowID,* into #tabl1 from V_QM_335 where id>=276 --获取你要的数据插入到一个临时表中
select * from #tabl1
Second step
Define the variable @i loop i, the total number of data in the @count table, and the ID of each loop @ID. Then query the total number from the table to @count. 101 pieces of data were found *
declare @i int=1,@count int,@ID varchar(30)
select @count=count(1) from #tabl1
select @count
third step:
The main content is the content of the while block. Inside, I first checked to see if the statement is correct. As shown below. After verifying that there is no problem, comment out the select statement, and then enable the exec statement block to execute statements in a loop. OK!!!
select ROW_NUMBER() OVER(ORDER BY ID)as RowID,* into #tabl1 from V_QM_335 where id>=276 --获取你要的数据插入到一个临时表中
select * from #tabl1
declare @i int=1,@count int,@ID varchar(30)
select @count=count(1) from #tabl1
select @count
while(@i<=@count)
begin
select @ID=V_335_1_10000005 from #tabl1 where RowID=@i
declare @strSql varchar(max)='update V_QM_340 set pid='+(select convert(varchar(10),max(ID)) from #tabl1 where V_335_1_10000005=@ID)+' where
V_340_1_10000005='''+@ID+''''
select(@strSql)
--exec(@strSql)
set @i+=1
end
备份给自己
SELECT ROW_NUMBER() OVER(ORDER BY PageCode) AS id ,PageCode,ColumnCode INTO #tabl1 FROM dbo.QM_ColumnsInfo WHERE ColumnCode = 10000709
–SELECT * FROM #tabl1
DECLARE @id INT=1
DECLARE @pagecode VARCHAR(100)=’’,@columncode VARCHAR(100)=’’
DECLARE @count int=0,@i int=1;
SELECT @count=COUNT(1) FROM #tabl1
WHILE(@i<@count)
BEGIN
SELECT @pagecode=PageCode,@columncode=ColumnCode FROM #tabl1 WHERE id=@i
DECLARE @sql VARCHAR(MAX)=’’
SET @columncode=‘V_’+@pagecode+‘1’+@columncode
SET @pagecode=‘V_QM_’+@pagecode
SET @sql=‘update ‘+@pagecode+’ set ‘+@columncode+’=REPLACE(’+@columncode+’,’‘qmUpload/file’’,’’/files/qmUpload/file’’)’
–SET @sql=‘select ‘+@columncode+’,REPLACE(’+@columncode+’,’‘qmUpload/file’’,’’/files/qmUpload/file’’) from '+@pagecode
print(@sql)
SET @i+=1
END
DROP TABLE #tabl1