SQL Server 表不使用默认值

2022-12-31数据库问题
1

本文介绍了SQL Server 表不使用默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 SSIS 包填充表格.

I am populating a table using an SSIS package.

这个想法是,每当包上传到表时,它都会使用 getdate() 对值进行时间戳.

The idea is that whenever the package uploads to the table, it will timestamp the value using getdate().

当我打开它时,我的 DDL 看起来是这样的:

My DDL looks like this when I open it up:

CREATE TABLE [REPORTING].[post_ssis_table_1](
    [validation_key] [int] IDENTITY(1,1) NOT NULL,
    [ssis_ran_date] [datetime] NULL,
    [server_name] [varchar](255) NULL,
    [data_base] [varchar](255) NULL,
--A bunch of other irrelevant columns
    [success_status] [varchar](255) NULL,
    [success_criteria] [varchar](255) NULL,
PRIMARY KEY CLUSTERED 
(
    [validation_key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [REPORTING].[post_ssis_tbl_1] ADD  DEFAULT (getdate()) FOR [ssis_ran_date]
GO

注意:我没有输入alter语句,这就是我打开DDL时的样子.

Note: I did not type the alter statement, that's literally what it looks like when I open up the DDL.

为什么在我插入行时没有为 [ssis_ran_date] 填充默认值?它实际上只是保持为空.

Why is the default value not populating for [ssis_ran_date] when I insert rows? It literally just stays null.

推荐答案

如果您的 INSERT 语句为具有默认值的列指定 NULL,则将插入 NULL.

If your INSERT statement specifies NULL for a column with a default, then NULL will get inserted.

只有在 INSERT 中根本没有指定该列时才会使用默认值.

The default will only be used when that column is not specified in the INSERT at all.

这篇关于SQL Server 表不使用默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12

sql group by 与不同
sql group by versus distinct(sql group by 与不同)...
2024-04-16 数据库问题
37

如何在SQL中返回每个组的增量组号
How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)...
2024-04-16 数据库问题
8

统计分组返回的记录数
Count number of records returned by group by(统计分组返回的记录数)...
2024-04-16 数据库问题
10