Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #Requires -Modules SqlServer
  2. #Requires -Modules ImportExcel
  3. <# The AdventureWorksDW2017 only has 29 tables and they're all under 1 million rows.#>
  4. cd SQLSERVER:\SQL\LocalHost\SQL2017\Databases\AdventureWorksDW2017\Tables
  5.  
  6. <# Scenario #1 A) all Dimensions in a single file,
  7. and B) each Fact table in their own file. #>
  8.  
  9. <# A) Every Dimension table in a worksheet named after the table, the same Excel file #>
  10. dir | WHERE { $_.name -like 'dim*' } |
  11. foreach {
  12. "$($_.Name)"
  13. Read-SqlTableData -ServerInstance LocalHost\SQL2017 -DatabaseName AdventureWorksDW2017 -SchemaName dbo -TableName $_.Name -OutputAs DataRows |
  14. Export-Excel -Path "c:\temp\AW\AdventureWorksDW2017_Dims.xlsx" -WorksheetName $_.Name -ExcludeProperty RowError,RowState,Table,ItemArray,HasErrors
  15. }
  16.  
  17. <# B) Each Fact-table in it's own Excel file, named after the table. #>
  18. dir | WHERE { $_.name -like 'fact*' } |
  19. foreach {
  20. "$($_.Name)"
  21. Read-SqlTableData -ServerInstance LocalHost\SQL2017 -DatabaseName AdventureWorksDW2017 -SchemaName dbo -TableName $_.Name -OutputAs DataRows |
  22. Export-Excel -Path "c:\temp\AW\$($_.Name).xlsx" -WorksheetName $_.Name -ExcludeProperty IsReadOnly,IsFixedSize,IsSynchronized,SyncRoot,Count
  23. }
  24.  
  25.  
  26. <# Scenario #2 Each table in it's own Excel file, named after the table. #>
  27. dir |
  28. foreach {
  29. "$($_.Name)"
  30. Read-SqlTableData -ServerInstance LocalHost\SQL2017 -DatabaseName AdventureWorksDW2017 -SchemaName dbo -TableName $_.Name -OutputAs DataRows |
  31. Export-Excel -Path "c:\temp\AW\$($_.Name).xlsx" -WorksheetName $_.Name -ExcludeProperty IsReadOnly,IsFixedSize,IsSynchronized,SyncRoot,Count
  32. }
  33.  
  34. cd SQLSERVER:\SQL\LocalHost\SQL2017\Databases\AdventureWorksDW2017\Tables
  35. <# Scenario #3 Every table in a worksheet named after the table, all in the same Excel file #>
  36. dir |
  37. foreach {
  38. "$($_.Name)"
  39. Read-SqlTableData -ServerInstance LocalHost\SQL2017 -DatabaseName AdventureWorksDW2017 -SchemaName dbo -TableName $_.Name -OutputAs DataRows |
  40. Export-Excel -Path "c:\temp\AW\AdventureWorksDW2017.xlsx" -WorksheetName $_.Name -ExcludeProperty RowError,RowState,Table,ItemArray,HasErrors
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement