简体   繁体   English

SQL Server 插入包含“时间戳(行版本)”列的表

[英]SQL Server Insert Into Table containing a column “Timestamp (Rowversion)”

I'm creating a C# Winforms application for recipe management in an industrial environment.我正在为工业环境中的配方管理创建一个 C# Winforms 应用程序。

I created a SQL Server table with 130 columns.我创建了一个包含 130 列的 SQL Server 表。 The table contains a column called CheckData (of datatype Timestamp ), which I use to detect changes made to a row.该表包含一个名为CheckData (数据类型Timestamp )的列,我用它来检测对行所做的更改。

If I insert a new row to that table all works fine.如果我在该表中插入新行,一切正常。 The code I use is:我使用的代码是:

INSERT INTO tablename (Column1, column2, column3, column4) 
VALUES (value1, value2, value3, value4)

I just assign values to major columns, the others get their default value.我只是为主要列分配值,其他列获得它们的默认值。 I do not assign a value to the timestamp field since it's written by the system.我没有为时间戳字段赋值,因为它是由系统写入的。

Additionally, I want to copy a row from this table to the same table (duplicate a data record).此外,我想将此表中的一行复制到同一个表中(复制数据记录)。

I copy the source row to a temporary table, drop the ID (primary key) and the timestamp fields in that temporary table and try to insert that only row in the temporary table into the table.我将源行复制到临时表,删除该临时表中的 ID(主键)和时间戳字段,并尝试将临时表中的该行仅插入到表中。 This fails.这失败了。

Here's the code:这是代码:

SELECT * 
INTO #temptable 
FROM tablename 
WHERE Recipe_No = 8;

ALTER TABLE #temptable DROP COLUMN ID, CHECKDATA;

ALTER TABLE #temptable REBUILD;

UPDATE #temptable 
SET Recipe_No = 9, Recipe_Name = 'Test' 
WHERE Recipe_No = 8;

INSERT INTO tablename 
    SELECT * FROM #temptable;

I don't understand where the difference is between inserting a new row thru INSERT INTO xxx (yyy) VALUES (zzz) and INSERT INTO xxx SELECT * FROM yyy .我不明白通过INSERT INTO xxx (yyy) VALUES (zzz)INSERT INTO xxx SELECT * FROM yyy插入新行之间的区别在哪里。 In both cases I don't try to write the timestamp value in the new row.在这两种情况下,我都不会尝试在新行中写入时间戳值。

Does anybody have an idea what I'm missing here?有人知道我在这里缺少什么吗?

I don't understand where the difference is between inserting a new row thru INSERT INTO xxx (yyy) VALUES (zzz) and INSERT INTO xxx SELECT * FROM yyy.我不明白通过 INSERT INTO xxx (yyy) VALUES (zzz) 和 INSERT INTO xxx SELECT * FROM yyy 插入新行之间的区别在哪里。

With this,有了这个,

INSERT INTO xxx SELECT * FROM yyy.

you are failing to specify the column mappings from the SELECT to the target table.您未能指定从 SELECT 到目标表的列映射。 You should always use你应该总是使用

INSERT INTO xxx (Column1, Column2, . . .)
SELECT (Column1, Column2, . . .)
FROM yyy

Here's a simplified example of what you're attempting:这是您尝试的简化示例:

drop table if exists t

create table t(id int, a int)
insert into t(id,a) values (1,1)

select * into #t from t where id = 1

alter table #t drop column id

insert into t select * from #t 

and it will fail with它会失败

Msg 213, Level 16, State 1, Line 12
Column name or number of supplied values does not match table definition.

because the temp table doesn't even have the same number of columns.因为临时表甚至没有相同数量的列。 And even if it did, you wouldn't know for sure that the column mappings were correct.即使这样做了,您也无法确定列映射是否正确。

It is failing because essentially your command它失败了,因为基本上你的命令

INSERT INTO tablename SELECT * FROM #temptable;";

Is telling SQL - "Insert everything into this table from this temp table."告诉 SQL - “将所有内容从这个临时表插入到这个表中。”

While you can work around this, I would say why don't you just try inserting into only the columns made available in your current table with only the values you would like to include.虽然您可以解决这个问题,但我想说您为什么不尝试仅将您想要包含的值插入到当前表中可用的列中。 Instead of needing to drop the columns/values, you just don't import it to begin with.不需要删除列/值,您只是不要从一开始就导入它。

An alternative - if you can write to a helper table, it may be beneficial to INSERT INTO that helper table, as opposed to a temp table, the values you have.另一种选择 - 如果您可以写入辅助表,则将您拥有的值INSERT INTO该辅助表(而不是临时表)可能是有益的。 Then transform that helper table, and THEN you can do INSERT INTO final_table SELECT * FROM helper .然后转换该辅助表,然后您可以执行INSERT INTO final_table SELECT * FROM helper This should give you the results you're looking for.这应该会给你你正在寻找的结果。

I hope this is helpful, and I hope it explains why your current command is failing.我希望这是有帮助的,我希望它解释了为什么您当前的命令失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM