4

I use the Script Generator which is integrated in the Microsoft SQL Server Management Studio to generate an import script for a whole database.

enter image description here enter image description here

I have to do some replacements in the script which I do with Powershell. Now I want to automate the generation. Is there a way to execute exactly this Script Generator Tool (and setting some options as on the screenshot - in my case 'Data only')? Or (if this isn't possible) can I open this tool window automatically from a ps script so I don't have to open the Management Studio, selecting the DB, ...?

I found some scripts which 'manually' build the script file in Powershell but that's not exactly what I'm looking for.

Thanks!

3
  • Maybe this answer will help you dba.stackexchange.com/questions/56616/… Commented Jul 28, 2016 at 11:28
  • You can create a SMO (SQL Server Management Objects) project kodyaz.com/sql-server-tools/… as shown in referred SQL tutorial for scripting database objects into a text file. Commented Jul 28, 2016 at 12:27
  • Hi and thanks for your hints and ideas! I want to try it via SMO but got a problem with it. I think it's a good idea to post a new question for this and I will post the link here. Commented Jul 29, 2016 at 9:43

1 Answer 1

7

This question's been here awhile and you've probably found your answer by now, but for those looking for a simple way to do this, the current versions of SQL server Powershell modules have native commands and methods that support this functionality from SMO.

You can use Get-SqlDatabase and methods such as .Script() and .EnumScript().

For example, this will generate CREATE scripts for user defined functions and save it to file:

$Database = Get-SqlDatabase -ServerInstance $YourSqlServer -Name $YourDatabaseName

$MyFuncs = $Database.UserDefinedFunctions | Where Schema -eq "dbo"
$MyFuncs.Script() | Out-File -FilePath ".\SqlScripts\MyFunctions.sql"

If you want to script data and elements like indexes, keys, triggers, etc. you will have to specify the scripting options, like this:

$scriptOptions = New-Object -TypeName Microsoft.SqlServer.Management.Smo.ScriptingOptions

$scriptOptions.NoCollation = $True
$scriptOptions.Indexes = $True
$scriptOptions.Triggers = $True
$scriptOptions.DriAll = $True
$scriptOptions.ScriptData = $True

$Database.Tables.EnumScript($scriptOptions) | Out-File -FilePath ".\AllMyTables.sql"

Note that the Script() method doesn't support scripting data. Use EnumScript() for tables.

If you want to script data only, as asked, you can try $scriptOptions.ScriptData = $True and $scriptOptions.ScriptSchema = $False.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.