Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come posso scorrere tutti i file in una cartella usando TSQL?

Ha fatto qualche ricerca e ha trovato un modo per scorrere i file usando qualcosa del genere:

CREATE TABLE #tmp(excelFileName VARCHAR(100));
INSERT INTO #tmp
EXEC xp_cmdshell 'dir /B c:\my\folder\path\';

declare @fileName varchar(100)

While (Select Count(*) From #tmp where excelFileName is not null) > 0
Begin

    Select Top 1 @fileName = excelFileName From #tmp

    -- OPENROWSET processing goes here, using @fileName to identify which file to use

    Delete from #tmp Where excelFileName = @FileName

End

DROP TABLE #tmp