sql將一個表中的數(shù)據(jù)插入到另一個表中的方法
更新時間:2014年03月22日 16:37:28 作者:
這篇文章主要介紹了sql將一個表中的數(shù)據(jù)插入到另一個表中的方法,需要的朋友可以參考下
列名不一定要相同,只要你在HH中列出要插入列的列表跟select from mm表中的選擇的列的列表一一對應就可以了,當然兩邊的數(shù)據(jù)類型應該是兼容的。
insert into hh (fielda,fieldb,fieldc) select fieldx,fieldy,fieldz from mm
---更新計量點中不存在的數(shù)據(jù),將臺帳中的信息轉移到計量點中
insert into MetricPoints (MeterID,MetricPointName,[Description],DepartmentID,MediumID)
(SELECT m.MeterID,m.MetricItems+m.InstallPlace as m_MetricPointName,m.MetricItems,m.DepartmentID,m.MediumID
FROM Meters m WHERE NOT EXISTS (SELECT 1 FROM MetricPoints WHERE MetricPoints.MeterID = m.MeterID ) and m.MediumID =2)
聲名:a,b ,都是表
--b表存在(兩表結構一樣)
insert into b select * from a
若兩表只是有部分(字段)相同,則
insert into b(col1,col2,col3,col4,...) select col1,col2,col3,col4,... from a where...
把表a插入到表b中去。
--b表不存在
select * into b from a
//
select (字段1,字段2,...) into b from a
insert into hh (fielda,fieldb,fieldc) select fieldx,fieldy,fieldz from mm
復制代碼 代碼如下:
---更新計量點中不存在的數(shù)據(jù),將臺帳中的信息轉移到計量點中
insert into MetricPoints (MeterID,MetricPointName,[Description],DepartmentID,MediumID)
(SELECT m.MeterID,m.MetricItems+m.InstallPlace as m_MetricPointName,m.MetricItems,m.DepartmentID,m.MediumID
FROM Meters m WHERE NOT EXISTS (SELECT 1 FROM MetricPoints WHERE MetricPoints.MeterID = m.MeterID ) and m.MediumID =2)
聲名:a,b ,都是表
復制代碼 代碼如下:
--b表存在(兩表結構一樣)
insert into b select * from a
若兩表只是有部分(字段)相同,則
復制代碼 代碼如下:
insert into b(col1,col2,col3,col4,...) select col1,col2,col3,col4,... from a where...
把表a插入到表b中去。
復制代碼 代碼如下:
--b表不存在
select * into b from a
//
select (字段1,字段2,...) into b from a
相關文章
SQLServer 優(yōu)化SQL語句 in 和not in的替代方案
用IN寫出來的SQL的優(yōu)點是比較容易寫及清晰易懂,這比較適合現(xiàn)代軟件開發(fā)的風格。2010-04-04實例理解SQL中truncate和delete的區(qū)別
這篇文章主要介紹了實例理解SQL中truncate和delete的區(qū)別,truncate和delete兩者易混,本文就為大家進行區(qū)分兩者的異同,感興趣的小伙伴們可以參考一下2016-02-02動態(tài)給表添加刪除字段并同時修改它的插入更新存儲過程
有一個表,用戶需要在后臺操作它,希望能對它動態(tài)進行添加刪除字段2011-11-11