Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Import-module "sqlps" -DisableNameChecking
- $serverInstance = "xxx,1433"
- $database = "xxx"
- $username = "xxx"
- $password = "xxx"
- $outputPathFormat = "C:\path_to_store_output\{0}.csv"
- $tableNamespaceFormat = "xxx.xxx.{0}"
- $tableNames = Invoke-Sqlcmd -ServerInstance $serverInstance -Username $username -Password $password -Database $database -Query "SELECT name FROM sys.tables" | Select-Object -ExpandProperty "Name"
- for ($i=0; $i -lt $tableNames.length; $i++) {
- $tableName = $tableNames[$i]
- $fullTableNamespace =[string]::Format($tableNamespaceFormat,$tableName)
- $query = [string]::Format("SELECT * FROM {0}", $fullTableNamespace)
- write-host $query
- Invoke-SqlCmd -ServerInstance $serverInstance -Username $username -Password $password -Database $database -Query $query | Export-Csv ([string]::Format($outputPathFormat, $fullTableNamespace)) -NoTypeInformation
- }
- Write-Host "Export job completed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement