SQLServerPSModule icon indicating copy to clipboard operation
SQLServerPSModule copied to clipboard

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

Open dbaileyut opened this issue 4 months ago • 0 comments

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.

dbaileyut avatar Aug 28 '25 17:08 dbaileyut