pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

將數(shù)據(jù)從一張表導(dǎo)入到另一張表

import data from one table to another table(將數(shù)據(jù)從一張表導(dǎo)入到另一張表)
本文介紹了將數(shù)據(jù)從一張表導(dǎo)入到另一張表的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我使用的是 SQL Server 2008 Enterprise.我需要將服務(wù)器/實(shí)例Server Foo"、數(shù)據(jù)庫Foo"和表Foo"中的所有數(shù)據(jù)導(dǎo)入目標(biāo)服務(wù)器/實(shí)例Server Goo"、數(shù)據(jù)庫Goo"和表Goo".Table Foo 和 Table Goo 具有相同的架構(gòu).如果表 Goo 存在相同的行,我想保留 Goo 中的原始數(shù)據(jù)并忽略 Foo 中的導(dǎo)入行(表 Foo 和表 Goo 都有一個(gè)名為 CustomerID 的唯一標(biāo)識(shí)符類型列,用作主鍵和聚集索引),只是就像忽略重復(fù)鍵一樣.

I am using SQL Server 2008 Enterprise. I need to import all data from Server/Instance "Server Foo", Database "Foo" and table "Foo", into destination Server/Instance "Server Goo", Database "Goo" and table "Goo". Table Foo and Table Goo are of the same schema. If the same row exists for table Goo, I want to keep the origin data in Goo and ingore the import row in Foo (table Foo and table Goo both has a uniqueidentifier type column called CustomerID which acts as primary key and clustered index), just like ignore duplicate key does.

我正在尋找簡單可靠的方法來編寫 T-SQL 來解決數(shù)據(jù)導(dǎo)出/導(dǎo)入問題.有參考樣本嗎?

I am looking for simple and reliable ways to write T-SQL to solve data export/import issue. Any reference samples?

編輯 1:

我使用 MERGE 嘗試了以下解決方案,但遇到了來自 SQL Server Management Studio 的以下錯(cuò)誤.任何想法出了什么問題?

I have tried the below solution using MERGE, but met with the following error from SQL Server Management Studio. Any ideas what is wrong?

更多信息:

LabTest1\SQLServer2008 => 服務(wù)器\實(shí)例名稱;OrderDB => 數(shù)據(jù)庫名稱;dbo => 模式名稱;訂單 => 表名.

LabTest1\SQLServer2008 => Server\Instance name; OrderDB => DB name; dbo => schema name; Orders => Table name.

merge into [dbo].[Orders] as Target
using "LabTest1\SQLServer2008.OrderDB.dbo.Orders" as source
on target.Hash = source.Hash
when not matched then
INSERT     ([Hash]
           ,[Order]
           ,[Name]
           ,[CreationTime]
           ,[Description])
     VALUES
     (
     source.[Hash], source.[Order], source.[Name], source.[CreationTime], source.[Description]
     )
when MATCHED then
;

錯(cuò)誤信息:

消息 102,級(jí)別 15,狀態(tài) 1,第 16 行';' 附近的語法不正確.

Msg 102, Level 15, State 1, Line 16 Incorrect syntax near ';'.

提前致謝,喬治

推薦答案

在 SQL Server 2008 中,您可以在 SQL Server Mgmt studio 中編寫 Goo.Goo 表的腳本,并告訴它創(chuàng)建一個(gè)腳本來插入所有數(shù)據(jù),使用T-SQL INSERT 語句.轉(zhuǎn)到對(duì)象資源管理器,右鍵單擊數(shù)據(jù)庫,選擇任務(wù) > 生成腳本",選擇要為其生成數(shù)據(jù)插入語句的表,并確保在此處使用此選項(xiàng):

In SQL Server 2008, you could script out your Goo.Goo table in SQL Server Mgmt studio and tell it to also create a script to insert all data by using T-SQL INSERT statements. Go the the Object Explorer, right-click on the database, pick "Tasks > Generate Scripts", pick the table you want to generate the data insert statements for, and make sure to use this option here:

然后可以在另一臺(tái)服務(wù)器上運(yùn)行它們以插入表格內(nèi)容.但是,在這種情況下,您必須自己處理插入可能的現(xiàn)有行.

Those could then be run on the other server to insert the table contents. In this case, however, you'd have to handle inserting possible existing rows yourself.

另一方面,如果兩臺(tái)服務(wù)器在同一網(wǎng)絡(luò)上,您可以使用鏈接服務(wù)器"功能并將源服務(wù)器鏈接到目標(biāo)服務(wù)器,然后使用 SQL Server 2008 MERGE 語句導(dǎo)入所有數(shù)據(jù)從源服務(wù)器的表到目標(biāo)服務(wù)器.

On the other hand, if both servers are on the same network, you could just use the "Linked Server" feature and link the source server to the target server and then use the SQL Server 2008 MERGE statement to import all the data from the source srever's table into the target server.

在對(duì)象資源管理器中,轉(zhuǎn)到服務(wù)器對(duì)象",然后鏈接服務(wù)器",右鍵單擊并添加新鏈接服務(wù)器"以在兩個(gè)服務(wù)器之間建立連接:

In the Object Explorer, go to "Server Objects", then "Linked Servers", right-click and "Add new linked server" to establish a connection between the two servers:

一旦服務(wù)器被鏈接,一個(gè)簡單的 MERGE 語句(SQL Server 2008 中的新增功能)將允許您合并這兩個(gè)表中的數(shù)據(jù):

Once the servers are linked, a simple MERGE statement (new in SQL Server 2008) will allow you to merge the data from those two tables:

MERGE 
  INTO Goo.Goo as Target
  USING Foo.Foo.dbo.Foo as Source
  ON Source.ID = Target.ID
WHEN NOT MATCHED THEN
  INSERT (field1, field2, field3)
  VALUES (source.field1, source.field2, source.field3)  
WHEN MATCHED THEN
  -- do nothing
;

在此處閱讀有關(guān)新 MERGE 語句的更多信息:

Read more about the new MERGE statement here:

  • http://www.builderau.com.au/program/sqlserver/soa/Using-SQL-Server-2008-s-MERGE-statement/0,339028455,339283059,00.htm
  • http://www.sqlservercentral.com/articles/Advanced+Querying/3122/

或在 SQL Server 2008 聯(lián)機(jī)叢書中.

or in the SQL Server 2008 Books Online.

馬克

這篇關(guān)于將數(shù)據(jù)從一張表導(dǎo)入到另一張表的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(hào)(問題和答案的組合))
主站蜘蛛池模板: 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 恒温水槽与水浴锅-上海熙浩实业有限公司 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | uv机-uv灯-uvled光固化机-生产厂家-蓝盾机电 | 并网柜,汇流箱,电控设备,中高低压开关柜,电气电力成套设备,PLC控制设备订制厂家,江苏昌伟业新能源科技有限公司 | 浙江浩盛阀门有限公司| 威海防火彩钢板,威海岩棉复合板,威海彩钢瓦-文登区九龙岩棉复合板厂 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 顺辉瓷砖-大国品牌-中国顺辉| 展厅设计公司,展厅公司,展厅设计,展厅施工,展厅装修,企业展厅,展馆设计公司-深圳广州展厅设计公司 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 医养体检包_公卫随访箱_慢病随访包_家签随访包_随访一体机-济南易享医疗科技有限公司 | 深圳市人通智能科技有限公司 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 无味渗透剂,泡沫抑尘剂,烷基糖苷-威海威能化工有限公司 | 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 档案密集架,移动密集架,手摇式密集架,吉林档案密集架-厂家直销★价格公道★质量保证 | 山东PE给水管厂家,山东双壁波纹管,山东钢带增强波纹管,山东PE穿线管,山东PE农田灌溉管,山东MPP电力保护套管-山东德诺塑业有限公司 | 卫浴散热器,卫浴暖气片,卫生间背篓暖气片,华圣格浴室暖气片 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 冷却塔风机厂家_静音冷却塔风机_冷却塔电机维修更换维修-广东特菱节能空调设备有限公司 | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 找果网 | 苹果手机找回方法,苹果iPhone手机丢了找回,认准找果网! | 电池高低温试验箱-气态冲击箱-双层电池防爆箱|简户百科 | 破碎机锤头_耐磨锤头_合金锤头-鼎成机械一站式耐磨铸件定制服务 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 丽陂特官网_手机信号屏蔽器_Wifi信号干扰器厂家_学校考场工厂会议室屏蔽仪 | 船老大板材_浙江船老大全屋定制_船老大官网 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 诺冠气动元件,诺冠电磁阀,海隆防爆阀,norgren气缸-山东锦隆自动化科技有限公司 | 电位器_轻触开关_USB连接器_广东精密龙电子科技有限公司 | 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 无菌水质袋-NASCO食品无菌袋-Whirl-Pak无菌采样袋-深圳市慧普德贸易有限公司 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 股票入门基础知识_股票知识_股票投资大师_格雷厄姆网 | 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 大型果蔬切片机-水果冬瓜削皮机-洗菜机切菜机-肇庆市凤翔餐饮设备有限公司 | 科普仪器菏泽市教育教学仪器总厂|