Abaixo um exemplo para converter os dados em massa sem ter que quebrar muito a cabeça, utilizando script em SQL2014:
USE DB
GO
SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO
ALTER
PROCEDURE [dbo].[TESTE001]
AS
BEGIN
DECLARE
@name VARCHAR(150)
DECLARE
@alias VARCHAR(150)
DECLARE
@SQL VARCHAR(150)
DECLARE
db_cursor CURSOR FOR
select
name from sys.tables where UPPER(name) not like '%99%' ORDER BY 1
CREATE
TABLE #TESTE001(DS_TAB VARCHAR (20) NOT NULL, NM_REG INT NOT NULL );
OPEN
db_cursor
FETCH
NEXT FROM db_cursor INTO @name
WHILE
@@FETCH_STATUS = 0
BEGIN
set @SQL
= 'INSERT INTO #TESTE001 SELECT ''' + @name + ''', COUNT(*) FROM ' +
@name
exec(@SQL)
FETCH
NEXT FROM db_cursor INTO @name
END
CLOSE
db_cursor
DEALLOCATE
db_cursor
DECLARE
db_curtst CURSOR FOR
select
DS_TAB from #TESTE001 where NM_REG <> 0
OPEN
db_curtst
FETCH
NEXT FROM db_curtst INTO @alias
WHILE
@@FETCH_STATUS = 0
BEGIN
--set
@SQL = 'DELETE FROM dbo.' + @alias + ' WHERE ' + SUBSTRING(@alias,2,2) +
'_FILIAL <> ''010101'' AND ' + SUBSTRING(@alias,2,2) +'_FILIAL
<> ''010102'' '
--print(@SQL)
--exec(@SQL)
set @SQL
= 'UPDATE dbo.' + @alias + ' SET ' + SUBSTRING(@alias,2,2) + '_FILIAL = ''01''
WHERE ' + SUBSTRING(@alias,2,2) + '_FILIAL = ''010101'' '
--print(@SQL)
exec(@SQL)
--set
@SQL = 'UPDATE dbo.' + @alias + ' SET ' + SUBSTRING(@alias,2,2) + '_FILIAL =
''02'' WHERE ' + SUBSTRING(@alias,2,2) + '_FILIAL = ''010102'' '
--print(@SQL)
--exec(@SQL)
FETCH
NEXT FROM db_curtst INTO @alias
END
CLOSE
db_curtst
DEALLOCATE
db_curtst
END
exec
[dbo].[TESTE001]
Nenhum comentário:
Postar um comentário