bibi92
asked on
script powershell to generate ddl table
Hello,
I search a powershell script to generate ddl tables for a database.
Thanks
Regards
I search a powershell script to generate ddl tables for a database.
Thanks
Regards
Can you show an example? "DDL tables" is something I cannot decode. DDL creates or changes database object definitions (add columns, add table, add constraint, ...).
ASKER
Hello to Generate script for Each table définition containing column and filegroup.
Thanks
Thanks
The features of SSMS to generate scripts is not sufficient?
ASKER
No I have to use PowerShell. Thanks
This can get pretty involved. A "simple" attempt, certainly dumping too much, is this:
$server = "MSSQLServer\Instance"
$srcDB = "TestDB"
$dump = "C:\Temp\EE\TestDB.sql"
# Stop on any error
$ErrorActionPreference = "stop"
[void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')
[void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SQlServer.SMOExtended')
$xfer = [Microsoft.SqlServer.Management.SMO.Transfer] ([Microsoft.SqlServer.Management.SMO.Server] $server).Databases[$srcDB]
# Set export options.
$opts = New-Object Microsoft.SqlServer.Management.SMO.ScriptingOptions
$opts.Filename = $dump
$opts.ToFileOnly = $true
$opts.AllowSystemObjects = $false
$opts.Statistics = $false
$opts.ScriptDataCompression = $false
$xfer.options = $opts
$xfer.ScriptTransfer()
It is based on scripts and explanations found at https://www.simple-talk.com/sql/database-administration/automated-script-generation-with-powershell-and-smo/. That site also shows how to get specific objects only (see "Automated scripting of objects").
ASKER
Thanks
Hello,
I search to script all tables with SMO from $liste_table but only the first table is generated on the file tbl_ddl.sql
$SQLSERVER="TEST\TEST"
$Database="DB"
$Schema="dbo"
[System.Reflection.Assembl y]::LoadWi thPartialN ame('Micro soft.SqlSe rver.SMO') | out-null
$s = new-object ('Microsoft.SqlServer.Mana gement.Smo .Server') "$SQLSERVER"
$db=$s.Databases["$Databas e"]
$liste_table = (& sqlcmd -E -S $SQLSERVER -h -1 -b -W -Q "set nocount on;select object_name from [DBA].[dbo].[ddl_configura tion] where new_fg_name <> 'NULL' and new_fg_name <> ''")
echo $liste_table
foreach($table in $liste_table)
{
$tbls = $db.Tables
$tbl = $tbls | where { $_.Schema -eq "$Schema" -and $_.Name -eq $table }
$scrp = new-object ('Microsoft.SqlServer.Mana gement.Smo .Scripter' ) ($s)
$scrp.Options.DriPrimaryKe y=$true
$scrp.Options.FileName = "D:\MSSQLSERVER\scripts\DE V\ddl\tbl_ ddl.sql"
$scrp.Options.ToFileOnly = $True
$scrp.Script($tbl)
}
How can I resolve, please ?
Thanks
Regards
Hello,
I search to script all tables with SMO from $liste_table but only the first table is generated on the file tbl_ddl.sql
$SQLSERVER="TEST\TEST"
$Database="DB"
$Schema="dbo"
[System.Reflection.Assembl
$s = new-object ('Microsoft.SqlServer.Mana
$db=$s.Databases["$Databas
$liste_table = (& sqlcmd -E -S $SQLSERVER -h -1 -b -W -Q "set nocount on;select object_name from [DBA].[dbo].[ddl_configura
echo $liste_table
foreach($table in $liste_table)
{
$tbls = $db.Tables
$tbl = $tbls | where { $_.Schema -eq "$Schema" -and $_.Name -eq $table }
$scrp = new-object ('Microsoft.SqlServer.Mana
$scrp.Options.DriPrimaryKe
$scrp.Options.FileName = "D:\MSSQLSERVER\scripts\DE
$scrp.Options.ToFileOnly = $True
$scrp.Script($tbl)
}
How can I resolve, please ?
Thanks
Regards
Does the echo print more than one table? If yes, then everything should work, but you have to use different file names for each table. As-is you are overwriting tbl_ddl.sql, so only the last table's DDL code remains. Or you set $scrp.Options.AppendToFile = $true.
ASKER
Yes, but the following error is generated
Multiple ambiguous overloads found for "Script" and the argument count: "1".
At line:8 char:1
+ $scrp.Script($tbl)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBes t
Multiple ambiguous overloads found for "Script" and the argument count: "1".
At line:8 char:1
+ $scrp.Script($tbl)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Which one of the two suggestions works?