Skip to content

Instantly share code, notes, and snippets.

@andrerocker
Created October 30, 2010 22:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrerocker/655798 to your computer and use it in GitHub Desktop.
Save andrerocker/655798 to your computer and use it in GitHub Desktop.
Powershell Script to: Export all data from yours SQLServer like mysqldump
$database_host = "<host>"
$database_name = "<database>"
$output_file = "<output_file>"
$user = "<username>"
$password = "<password>"
[system.reflection.assembly]::loadWithPartialName('Microsoft.SqlServer.SMO')
$server = new-object "Microsoft.SqlServer.Management.Smo.Server" $database_host
$server.connectionContext.loginSecure = $false
$server.connectionContext.set_Login($user)
$server.connectionContext.set_Password($password)
$database = $server.databases[$database_name]
$database.script() | out-file $output_file
$scripter = new-object "Microsoft.SqlServer.Management.Smo.Scripter" $server
$scripter.options.appendToFile = $false
$scripter.options.scriptData = $true
$scripter.options.includeIfNotExists = $true
$scripter.options.includeHeaders = $true
$scripter.options.indexes = $true
$scripter.options.withDependencies = $true
$scripter.options.toFileOnly = $true
$scripter.options.fileName = $output_file
$tables = $database.tables
foreach ($s in $scripter.enumScript($tables)) { write-host $s }
@kiquenet
Copy link

How-to export 100 rows (for eg.) each table in database, not all data ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment