Skip to content

Commit

Permalink
New-DbaDbTable - Add DefaultEx to support functions as defaults for s…
Browse files Browse the repository at this point in the history
…tring types (#8142)
  • Loading branch information
andreasjordan committed Feb 4, 2022
1 parent 5fa8351 commit d0ea48a
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion functions/New-DbaDbTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ function New-DbaDbTable {
Creates a new table on sql2017 in tempdb with the name testtable and one column
.EXAMPLE
PS C:\> $col = @{
>> Name = 'Id'
>> Type = 'varchar'
>> MaxLength = 36
>> DefaultEx = 'NEWID()'
>> }
PS C:\> New-DbaDbTable -SqlInstance sql2017 -Database tempdb -Name testtable -ColumnMap $col
Creates a new table on sql2017 in tempdb with the name testtable and one column. Uses "DefaultEx" to interpret the value as an expression and not as a string.
.EXAMPLE
PS C:\> # Create collection
>> $cols = @()
Expand Down Expand Up @@ -427,7 +438,15 @@ function New-DbaDbTable {
$sqlColumn = New-Object Microsoft.SqlServer.Management.Smo.Column $object, $column.Name, $dataType
$sqlColumn.Nullable = $column.Nullable

if ($column.Default) {
if ($column.DefaultEx) {
# override the default that would add quotes to an expression
if ($column.DefaultName) {
$dfName = $column.DefaultName
} else {
$dfName = "DF_$name`_$($column.Name)"
}
$sqlColumn.AddDefaultConstraint($dfName).Text = $column.DefaultEx
} elseif ($column.Default) {
if ($column.DefaultName) {
$dfName = $column.DefaultName
} else {
Expand Down

0 comments on commit d0ea48a

Please sign in to comment.