SQL Server:组合两个表

时间:2013-12-18 11:27:26

标签: sql sql-server

我有2张大表,每张约25列。它们都包含相同类型的数据,但列的顺序不同。

如何组合这些表而无需重新排列查询代码?对于~600行脚本,我宁愿不这样做。

如果需要,我可以给出一个示例,但我真的没有看到它的需要。

我到目前为止已经尝试过了;

SELECT * FROM [guest].table1 UNION ALL SELECT * FROM [guest].table2;
SELECT * FROM [guest].table1, [guest].table2;

我也试过像这样插入数据;

SET IDENTITY_INSERT [guest].table1 ON;
SET IDENTITY_INSERT [guest].table2 ON;

INSERT INTO [guest].table1 id,  short_name,  name,  invention_title, reference,  client_id,  client_ref,  date_case_opened,  date_case_closed,  case_type,  notes,  fee_earner,  created,  last_updated,  file_location,  foreign_attorney_id,  foreign_attorney_ref,  country_code,  application_number,  filing_date,  publication_number,  invoice_currency,  publication_date,  status,  pct_application_number,  pct_case_id,  national_phase_entry,  base_number,  base_country,  base_date,  base_case_id,  divisional_date_lodged,  illustrative_image,  parent_case_id,  parent_application_number)
select id,  short_name,  name,  invention_title, reference,  client_id,  client_ref,  date_case_opened,  date_case_closed,  case_type,  notes,  fee_earner,  created,  last_updated,  file_location,  foreign_attorney_id,  foreign_attorney_ref,  country_code,  application_number,  filing_date,  publication_number,  invoice_currency,  publication_date,  status,  pct_application_number,  pct_case_id,  national_phase_entry,  base_number,  base_country,  base_date,  base_case_id,  divisional_date_lodged,  illustrative_image,  parent_case_id,  parent_application_number
FROM [guest].table2;

但是这给了我这个错误:

  

SQL错误:当IDENTITY_INSERT设置为OFF时,无法在表'exported_cases'中为identity列插入显式值。

非常感谢任何帮助!

为了澄清,我正在导出一个当前以2个部分存在的数据库(table1和table2)。它已经生成了PK,并且在它完全导出之前我没有插入它,此时PK将设置为高于任何当前PK的数字。

(我需要保留ID字段。)

4 个答案:

答案 0 :(得分:1)

由于每个人都专注于插入,让我回答选择部分:)

要使UNION正常工作,请确保以相同的顺序显式选择所有列:

SELECT id,  short_name,  name,  invention_title, reference,  client_id,  client_ref,  date_case_opened,  date_case_closed,  case_type,  notes,  fee_earner,  created,  last_updated,  file_location,  foreign_attorney_id,  foreign_attorney_ref,  country_code,  application_number,  filing_date,  publication_number,  invoice_currency,  publication_date,  status,  pct_application_number,  pct_case_id,  national_phase_entry,  base_number,  base_country,  base_date,  base_case_id,  divisional_date_lodged,  illustrative_image,  parent_case_id,  parent_application_number
FROM [guest].table1
UNION ALL
SELECT id,  short_name,  name,  invention_title, reference,  client_id,  client_ref,  date_case_opened,  date_case_closed,  case_type,  notes,  fee_earner,  created,  last_updated,  file_location,  foreign_attorney_id,  foreign_attorney_ref,  country_code,  application_number,  filing_date,  publication_number,  invoice_currency,  publication_date,  status,  pct_application_number,  pct_case_id,  national_phase_entry,  base_number,  base_country,  base_date,  base_case_id,  divisional_date_lodged,  illustrative_image,  parent_case_id,  parent_application_number
FROM [guest].table2

此结果集可用于查询,插入新表或其他任何内容:)

答案 1 :(得分:0)

    you should not insert identity element its generated auto:

just try inserting all except id(which is your identity)

请试试这个:

    SET IDENTITY_INSERT [guest].table1 ON;
    SET IDENTITY_INSERT [guest].table2 ON;

    INSERT INTO [guest].table1 id,  short_name,  name,  invention_title, reference,  client_id,  client_ref,  date_case_opened,  date_case_closed,  case_type,  notes,  fee_earner,  created,  last_updated,  file_location,  foreign_attorney_id,  foreign_attorney_ref,  country_code,  application_number,  filing_date,  publication_number,  invoice_currency,  publication_date,  status,  pct_application_number,  pct_case_id,  national_phase_entry,  base_number,  base_country,  base_date,  base_case_id,  divisional_date_lodged,  illustrative_image,  parent_case_id,  parent_application_number)
    select id,  short_name,  name,  invention_title, reference,  client_id,  client_ref,  date_case_opened,  date_case_closed,  case_type,  notes,  fee_earner,  created,  last_updated,  file_location,  foreign_attorney_id,  foreign_attorney_ref,  country_code,  application_number,  filing_date,  publication_number,  invoice_currency,  publication_date,  status,  pct_application_number,  pct_case_id,  national_phase_entry,  base_number,  base_country,  base_date,  base_case_id,  divisional_date_lodged,  illustrative_image,  parent_case_id,  parent_application_number
    FROM [guest].table2;

SET IDENTITY_INSERT [guest].table1 OFF;
SET IDENTITY_INSERT [guest].table2 OFF;

答案 2 :(得分:0)

您已设置Identity Specification-> Is Identity to True

所以你不能手动插入自动生成的主键!!!

答案 3 :(得分:0)

INSERT INTO   short_name,  name,  invention_title, reference,  client_id,  client_ref,  date_case_opened,  date_case_closed,  case_type,  notes,  fee_earner,  created,  last_updated,  file_location,  foreign_attorney_id,  foreign_attorney_ref,  country_code,  application_number,  filing_date,  publication_number,  invoice_currency,  publication_date,  status,  pct_application_number,  pct_case_id,  national_phase_entry,  base_number,  base_country,  base_date,  base_case_id,  divisional_date_lodged,  illustrative_image,  parent_case_id,  parent_application_number)
select   short_name,  name,  invention_title, reference,  client_id,  client_ref,  date_case_opened,  date_case_closed,  case_type,  notes,  fee_earner,  created,  last_updated,  file_location,  foreign_attorney_id,  foreign_attorney_ref,  country_code,  application_number,  filing_date,  publication_number,  invoice_currency,  publication_date,  status,  pct_application_number,  pct_case_id,  national_phase_entry,  base_number,  base_country,  base_date,  base_case_id,  divisional_date_lodged,  illustrative_image,  parent_case_id,  parent_application_number
FROM [guest].table2;

在没有ID的情况下尝试此操作。