SSIS 插入不插入计算列

发布于 2025-01-01 14:39:28 字数 359 浏览 0 评论 0原文

我正在使用 SSIS 将 Excel 文件插入到 Sql Server 表中。我相信它使用批量插入,因此它不会插入到“CreationDate”和“ModificationDate”列(这两个列都是以 getdate() 作为默认值的计算列)。

有办法解决这个问题吗?

另外,需要明确的是 - 这两个日期列都不属于 Excel 的一部分。这是确切的场景:

我的 Excel 有两列 - 代码和描述。我的 SQL Server 表有 4 列代码、描述、创建日期、修改日期。

因此,当 SSIS 复制数据时,它会复制代码和描述,但 CreationDate 和 ModificationDate(它们是 SQL Server 计算列)都是空的。

I am using SSIS to insert a Excel file into a Sql Server Table. I believe it uses the Bulk insert, and as a result it doesn't insert into the 'CreationDate' and the 'ModificationDate' columns (both of which are computed columns with getdate() as the default).

Is there a way to get around this problem?

Also, just to be clear - both these date columns are not a part of excel. Here is the exact scenario:

My excel has two columns - code and description. My SQL Server table has 4 columns Code, Description, CreationDate, ModificationDate.

So, when the SSIS copies the data, it copies Code and Description, but the CreationDate and ModificationDate (which are SQL Server Computed Columns) are both empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

卖梦商人 2025-01-08 14:39:28

如果您想记录创建,您应该使用带有默认约束的普通列
计算列定义为 每次查询时,GETDATE()都会改变。

计算列也不可能被填充

因此,假设您的意思是“默认的普通列”,那么您需要停止从 SSIS 发送 NULL ,这将覆盖默认值

这一切都在这里演示:

CREATE TABLE #foo (
   bar int NOT NULL,
   testCol1Null datetime NULL DEFAULT GETDATE(),
   testCol1NotNull datetime NOT NULL DEFAULT GETDATE(),
   testCol2 AS GETDATE()
   );
INSERT #foo (bar, testCol1Null) VALUES (1, NULL);

SELECT * FROM #foo;
WAITFOR DELAY '00:00:00.100';
SELECT * FROM #foo;
WAITFOR DELAY '00:00:00.100';
SELECT * FROM #foo;

DROP TABLE #foo;

假设您正在使用 SSIS 中的批量插入任务,则需要设置“Keep nulls = off/选项页面中未选中”

You should use a normal column with a default constraint if you want to log creation
A computed column defined as GETDATE() will change every time you query it.

It is also impossible for a computed column to not be populated

So, assuming you mean "normal column with default", then you need stop sending NULL from SSIS which overrides the default

This is all demonstrated here:

CREATE TABLE #foo (
   bar int NOT NULL,
   testCol1Null datetime NULL DEFAULT GETDATE(),
   testCol1NotNull datetime NOT NULL DEFAULT GETDATE(),
   testCol2 AS GETDATE()
   );
INSERT #foo (bar, testCol1Null) VALUES (1, NULL);

SELECT * FROM #foo;
WAITFOR DELAY '00:00:00.100';
SELECT * FROM #foo;
WAITFOR DELAY '00:00:00.100';
SELECT * FROM #foo;

DROP TABLE #foo;

Assuming you are using the Bulk Insert Task in SSIS, then you need to set "Keep nulls = off/unchecked" in the options page

尴尬癌患者 2025-01-08 14:39:28

您应该对指定 get 的列有一个默认约束。

col1 datetime default getdate()

还应该有一个用于批量插入 KEEPNULLS 的选项,该选项应该关闭。

来自MSDN 上的批量插入:

指定空列在批量导入操作期间应保留空值,而不是具有任何默认值
已插入列。有关详细信息,请参阅保留 Null 或使用
批量导入期间的默认值。

KEEPNULLS 也有记录: http://msdn.microsoft.com/en-us/库/ms187887.aspx

You should have a default constraint on the column(s) that specifies get

col1 datetime default getdate()

There should also be an option for the bulk insert KEEPNULLS which should be turned off.

From Bulk Insert on MSDN:

Specifies that empty columns should retain a null value during the bulk-import operation, instead of having any default values for the
columns inserted. For more information, see Keeping Nulls or Using
Default Values During Bulk Import.

KEEPNULLS is also documented: http://msdn.microsoft.com/en-us/library/ms187887.aspx

峩卟喜欢 2025-01-08 14:39:28

将派生列放入数据流中,并使用所需的值填充两个缺失的列。

Put in a Derived Column in your dataflow and populate the two missing columns with the values you want.

晒暮凉 2025-01-08 14:39:28

计算列上的值实际上并不存在于数据库中,每次 SQL Server 需要访问它时都会计算它,这就是为什么您无法在插入时向其通知值。

您需要的是一个默认列,该列具有默认值,如果您不告知任何其他值,则会插入该默认值。

CreationDate datetime default getdate()
ModificationDate datetime default getdate()

The value on a computed column doesn't physically exists on the database, it is calculated every time SQL Server needs to access it, that's why you can't inform a value to it on a insert.

What you need is a default column, which is a column that has a default value that's inserted if you don't inform any other value.

CreationDate datetime default getdate()
ModificationDate datetime default getdate()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文