我有2個大表,每個大約25列。它們都包含相同類型的數據,但列的順序不同。SQL Server:結合兩個表
如何組合這些表而無需重新排列我的查詢代碼?我寧願不要爲〜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 Error: Cannot insert explicit value for identity column in table 'exported_cases' when IDENTITY_INSERT is set to OFF.
任何幫助將不勝感激!
爲了澄清,我導出了一個當前存在於2個部分中的數據庫(表1和表2)。它已經生成了PK,並且在它完全導出之前我沒有插入它,此時PK將被設置爲比任何當前PK更高的數字。
(我需要保持保留ID字段。)
既然你在IDENTITY_INSERT標識符列中插入它,它需要你提到你插入到的所有列名,並提供一個值標識列,讓標識列生成值s,因爲它是你的代碼容易出錯,因爲你最終可能在Identity列中出現重複。 –