Skip to content

Write-SqlTableData doesn't support omitting columns with default values #112

@dbaileyut

Description

@dbaileyut

With something like this:

CREATE TABLE [dbo].[TestTable1](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[ChangeDateTime] [datetime] NOT NULL,
	[Category] [nvarchar](64) NOT NULL
 CONSTRAINT [PK_TestTable1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
) ON [PRIMARY]
)

ALTER TABLE [dbo].[TestTable1] ADD  CONSTRAINT [DF_TestTable_ChangeDateTime]  DEFAULT (getdate()) FOR [ChangeDateTime]
GO

In SQL, I can do this:

INSERT INTO [dbo].[TestTable1]
        (
            [Name], 
            [Category]
        )
     VALUES 
        (
           'MyName1'
           ,'Blue'
        )
GO

I expect (perhaps with an additional parameter, CheckConstraints?) to be able to do this:

$PSObj = [PSCustomObject]@{
    Id = ''
    Name = 'FromPwsh'
    ChangeDateTime = ''
    Category = 'Red'
}
Write-SqlTableData -InputData $PSObj  -ServerInstance $SqlServer -DatabaseName $DatabaseName -TableName $TableName -SchemaName $SchemaName

but that fails with

Write-SqlTableData : The given value '' of type String from the data source cannot be converted to type datetime for Column 2 [ChangeDateTime] Row 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions