Слайд 49Upgrade Advisor Import 2
--This script imports MOST Upgrade Advisor DE*.CSV reports.
--It
will need modification in some cases.
use DBOps
go
--for demo
delete from importDE
set nocount on
declare @fileExt char(3)
set @fileExt = 'csv'
truncate table UAfileImport
declare @serverList table (serverName varchar(20))
declare @fileList table (fileName varchar(50))
declare @fileName varchar(15)
declare @uncFilePath varchar(100)
declare @uncServerUpgradeFilePath varchar(120)
declare @serverName varchar(20)
declare @engine varchar(20)
declare @statement varchar(1500)
declare @quote char(1)
declare @nsql nvarchar(1500)
declare @importFilePath varchar(100)
declare @trash table (col1 int)
declare @serversDE table (serverName varchar(39))
set @uncFilePath = 'C:\_SQLupgrade\UAoutput\'
set @quote = char(39)
set @statement = 'dir /AD /B /ON ' + @uncFilePath
insert @serverList (serverName)
exec master.dbo.xp_cmdshell @statement
delete @serverList where serverName is NULL
WHILE exists (select * from @serverList)
begin
set @serverName = (select top 1 serverName from @serverList)
delete from @serverList where serverName = @serverName
set @uncServerUpgradeFilePath = @uncFilePath + @serverName + '\'
set @statement = 'dir /A-D /B /ON ' + @uncServerUpgradeFilePath + '*.'+@fileExt
insert @fileList (fileName)
exec master.dbo.xp_cmdshell @statement
delete from @fileList where fileName is null
--for demo
delete from @fileList where left(filename,2)<> 'DE'
if exists (select * from @fileList where filename like 'DE%')
begin
set @fileName = (select top 1 fileName from @fileList)
delete from @fileList where fileName = @fileName
--rename the files (format of De.xml.csv will cause an error, so rename to De.csv)
set @statement = 'ren '+@uncServerUpgradeFilePath+@fileName+' '+left(@fileName,2)+'.'+@fileExt
insert @trash --discard result
exec master.dbo.xp_cmdshell @statement
select @fileName = left(@fileName,2)+'.'+@fileExt
--select 'Importing '+@serverName+', '+@filename
set @nsql = 'Insert importDE
SELECT serverName='+@quote+@serverName+@quote+',importDate=getdate(),F1,F2,F3,F4,F5,F6,F7
FROM OpenRowset('
+@quote+'Microsoft.Jet.OLEDB.4.0'+@quote+', '
+@quote+'Text;Database='
+ @uncServerUpgradeFilePath +';HDR=NO'+@quote+','
+@quote+'select * from '+@fileName+@quote+')'
exec sp_executesql @nsql
--some files have six columns
If not exists (select * from importDE where serverName = @serverName)
begin
select 'Retry 1 for '+@serverName+', '+@filename
set @nsql = 'Insert importDE serverName, importdate,F1,F2,F3,F4,F5,F6
SELECT serverName='+@quote+@serverName+@quote+',importDate=getdate(),F1,F2,F3,F4,F5,F6
FROM OpenRowset('
+@quote+'Microsoft.Jet.OLEDB.4.0'+@quote+', '
+@quote+'Text;Database='
+ @uncServerUpgradeFilePath +';HDR=NO'+@quote+','
+@quote+'select * from '+@fileName+@quote+')'
exec sp_executesql @nsql
end
--and, some have five columns
If not exists (select * from importDE where serverName = @serverName)
begin
select 'Retry 2 for '+@serverName+', '+@filename
set @nsql = 'Insert importDE serverName, importdate,F1,F2,F3,F4,F5
SELECT serverName='+@quote+@serverName+@quote+',importDate=getdate(),F1,F2,F3,F4,F5
FROM OpenRowset('
+@quote+'Microsoft.Jet.OLEDB.4.0'+@quote+', '
+@quote+'Text;Database='
+ @uncServerUpgradeFilePath +';HDR=NO'+@quote+','
+@quote+'select * from '+@fileName+@quote+')'
exec sp_executesql @nsql
end
If not exists (select * from importDE where serverName = @serverName)
select 'Server '+@serverName+' could not be imported.', @nsql
end
delete from @fileList
end
select distinct [Successfully imported:]= serverName from importDE
-- select * from DBOps.dbo.importDE
-- delete from importDE