0

我想在 SQL ServerVariables中复制行。VariablesTest两者具有相同的列、数据类型等。阅读下面的答案,我想这是要走的路

set IDENTITY_INSERT VariablesTest ON
insert VariablesTest Select * from Variables
set IDENTITY_INSERT VariablesTest OFF

但 SQL Server Management Studio 会引发错误:

An explicit value for the identity column in table 'VariablesTest' can only be specified when a column list is used and IDENTITY_INSERT is ON.

叹...

4

2 回答 2

1

"can only be specified when a column list is used"

The error message is telling you to use a column list:

set IDENTITY_INSERT VariablesTest ON
insert VariablesTest(column1,column2,etc) Select column1,column2,etc from Variables
set IDENTITY_INSERT VariablesTest OFF
于 2013-07-01T21:57:39.197 回答
0

If you know that the columns are the same in both tables you still need to list them for the INSERT portion but the SELECT portion can use an asterisk...

set IDENTITY_INSERT VariablesTest ON

INSERT VariablesTest (col1, col2, col3, col4,...) SELECT * FROM Variables

set IDENTITY_INSERT VariablesTest OFF

...this isn't stable code and if a column is added to both tables or just one it will break. So even though this is possible, listing both sets of columns separately is the preferred method. Using tools such as Red Gate's SQL Prompt can allow you to auto-expand your asterisks into comma delimited lists of columns, so that is also an option and one that I use frequently to save typing and mistakes.

于 2013-07-02T02:58:46.143 回答