Sql代码 

1.Oracle9i引入了MERGE命令,你能够在一个SQL语句中对一个表同时执行inserts和updates操作. MERGE命令从一个或多个数据源中选择行来updating或inserting到一个或多个表.在Oracle 10g中MERGE有如下一些改进: 

2. 

3.1、UPDATE或INSERT子句是可选的 

4. 

5.2、UPDATE和INSERT子句可以加WHERE子句 

6. 

7.3、在ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表 

8. 

9.4、UPDATE子句后面可以跟DELETE子句来去除一些不需要的行 

10. 

11.首先创建示例表: 

12. 

13.create table PRODUCTS 

14. ( 

15. PRODUCT_ID INTEGER, 

16. PRODUCT_NAME VARCHAR2(60), 

17. CATEGORY VARCHAR2(60) 

18. ); 

19. 

20. insert into PRODUCTS values (1501, 'VIVITAR 35MM', 'ELECTRNCS'); 

21. insert into PRODUCTS values (1502, 'OLYMPUS IS50', 'ELECTRNCS'); 

22. insert into PRODUCTS values (1600, 'PLAY GYM', 'TOYS'); 

23. insert into PRODUCTS values (1601, 'LAMAZE', 'TOYS'); 

24. insert into PRODUCTS values (1666, 'HARRY POTTER', 'DVD'); 

25. commit; 

26. 

27. create table NEWPRODUCTS 

28. ( 

29. PRODUCT_ID INTEGER, 

30. PRODUCT_NAME VARCHAR2(60), 

31. CATEGORY VARCHAR2(60) 

32. ); 

33. 

34. insert into NEWPRODUCTS values (1502, 'OLYMPUS CAMERA', 'ELECTRNCS'); 

35. insert into NEWPRODUCTS values (1601, 'LAMAZE', 'TOYS'); 

36. insert into NEWPRODUCTS values (1666, 'HARRY POTTER', 'TOYS'); 

37. insert into NEWPRODUCTS values (1700, 'WAIT INTERFACE', 'BOOKS'); 

38. commit; 

39. 

40.1、可省略的UPDATE或INSERT子句 

41. 

42.在Oracle 9i, MERGE语句要求你必须同时指定INSERT和UPDATE子句.而在Oracle 10g, 你可以省略UPDATE或INSERT子句中的一个. 下面的例子根据表NEWPRODUCTS的PRODUCT_ID字段是否匹配来updates表PRODUCTS的信息: 

43. 

44.SQL> MERGE INTO products p 

45. 2 USING newproducts np 

46. 3 ON (p.product_id = np.product_id) 

47. 4 WHEN MATCHED THEN 

48. 5 UPDATE 

49. 6 SET p.product_name = np.product_name, 

50. 7 p.category = np.category; 

51. 

52. 3 rows merged. 

53. 

54. SQL> SELECT * FROM products; 

55. 

56. PRODUCT_ID PRODUCT_NAME CATEGORY 

57. ---------- -------------------- ---------- 

58. 1501 VIVITAR 35MM ELECTRNCS 

59. 1502 OLYMPUS CAMERA ELECTRNCS 

60. 1600 PLAY GYM TOYS 

61. 1601 LAMAZE TOYS 

62. 1666 HARRY POTTER TOYS 

63. SQL> 

64. SQL> ROLLBACK; 

65. Rollback complete. 

66. SQL> 

67. 

68.在上面例子中, MERGE语句影响到是产品id为1502, 1601和1666的行. 它们的产品名字和种 类被更新为表newproducts中的值. 下面例子省略UPDATE子句, 把表NEWPRODUCTS中新的PRODUCT_ID插入到表PRODUCTS中, 对于在两个表中能够匹配上PRODUCT_ID的数据不作任何处理. 从这个例子你能看到PRODUCT_ID=1700的行被插入到表PRODUCTS中. 

69. 

70.SQL> MERGE INTO products p 

71. 2 USING newproducts np 

72. 3 ON (p.product_id = np.product_id) 

73. 4 WHEN NOT MATCHED THEN 

74. 5 INSERT 

75. 6 VALUES (np.product_id, np.product_name, 

76. 7 np.category); 

77. 

78. 1 row merged. 

79. 

80. SQL> SELECT * FROM products; 

81. 

82. PRODUCT_ID PRODUCT_NAME CATEGORY 

83. ---------- -------------------- ---------- 

84. 1501 VIVITAR 35MM ELECTRNCS 

85. 1502 OLYMPUS IS50 ELECTRNCS 

86. 1600 PLAY GYM TOYS 

87. 1601 LAMAZE TOYS 

88. 1666 HARRY POTTER DVD 

89. 1700 WAIT INTERFACE BOOKS 

90. 

91.2、带条件的Updates和Inserts子句 

92. 

93.你能够添加WHERE子句到UPDATE或INSERT子句中去, 来跳过update或insert操作对某些行的处理. 下面例子根据表NEWPRODUCTS来更新表PRODUCTS数据, 但必须字段CATEGORY也得同时匹配上: 

94. 

95.SQL> MERGE INTO products p 

96. 2 USING newproducts np 

97. 3 ON (p.product_id = np.product_id) 

98. 4 WHEN MATCHED THEN 

99. 5 UPDATE 

100. 6 SET p.product_name = np.product_name 

101. 7 WHERE p.category = np.category; 

102. 

103. 2 rows merged. 

104. 

105. SQL> SELECT * FROM products; 

106. 

107. PRODUCT_ID PRODUCT_NAME CATEGORY 

108. ---------- -------------------- ---------- 

109. 1501 VIVITAR 35MM ELECTRNCS 

110. 1502 OLYMPUS CAMERA ELECTRNCS 

111. 1600 PLAY GYM TOYS 

112. 1601 LAMAZE TOYS 

113. 1666 HARRY POTTER DVD 

114. SQL> 

115. SQL> rollback; 

116. 

117.在这个例子中, 产品ID为1502,1601和1666匹配ON条件但是1666的category不匹配. 因此MERGE命令只更新两行数据. 下面例子展示了在Updates和Inserts子句都使用WHERE子句: 

118. 

119.SQL> MERGE INTO products p 

120. 2 USING newproducts np 

121. 3 ON (p.product_id = np.product_id) 

122. 4 WHEN MATCHED THEN 

123. 5 UPDATE 

124. 6 SET p.product_name = np.product_name, 

125. 7 p.category = np.category 

126. 8 WHERE p.category = 'DVD' 

127. 9 WHEN NOT MATCHED THEN 

128. 10 INSERT 

129. 11 VALUES (np.product_id, np.product_name, np.category) 

130. 12 WHERE np.category != 'BOOKS' 

131. SQL> / 

132. 

133. 1 row merged. 

134. 

135. SQL> SELECT * FROM products; 

136. 

137. PRODUCT_ID PRODUCT_NAME CATEGORY 

138. ---------- -------------------- ---------- 

139. 1501 VIVITAR 35MM ELECTRNCS 

140. 1502 OLYMPUS IS50 ELECTRNCS 

141. 1600 PLAY GYM TOYS 

142. 1601 LAMAZE TOYS 

143. 1666 HARRY POTTER TOYS 

144. 

145. SQL> 

146. 

147.注意由于有WHERE子句INSERT没有插入所有不匹配ON条件的行到表PRODUCTS. 

148. 

149.3、无条件的Inserts 

150. 

151.你能够不用连接源表和目标表就把源表的数据插入到目标表中. 这对于你想插入所有行到目标表时是非常有用的. Oracle 10g现在支持在ON条件中使用常量过滤谓词. 举个常量过滤谓词例子ON (1=0). 下面例子从源表插入行到表PRODUCTS, 不检查这些行是否在表PRODUCTS中存在: 

152. 

153. 

154. 

155. 

156. 

157. 

158.SQL> MERGE INTO products p 

159. 2 USING newproducts np 

160. 3 ON (1=0) 

161. 4 WHEN NOT MATCHED THEN 

162. 5 INSERT 

163. 6 VALUES (np.product_id, np.product_name, np.category) 

164. 7 WHERE np.category = 'BOOKS' 

165. SQL> / 

166. 

167. 1 row merged. 

168. 

169. SQL> SELECT * FROM products; 

170. 

171. PRODUCT_ID PRODUCT_NAME CATEGORY 

172. ---------- -------------------- ---------- 

173. 1501 VIVITAR 35MM ELECTRNCS 

174. 1502 OLYMPUS IS50 ELECTRNCS 

175. 1600 PLAY GYM TOYS 

176. 1601 LAMAZE TOYS 

177. 1666 HARRY POTTER DVD 

178. 1700 WAIT INTERFACE BOOKS 

179. 6 rows selected. 

180. SQL> 

181. 

182.4、新增加的DELETE子句 

183. 

184.Oracle 10g中的MERGE提供了在执行数据操作时清除行的选项. 你能够在WHEN MATCHED THEN UPDATE子句中包含DELETE子句. DELETE子句必须有一个WHERE条件来删除匹配某些条件的行.匹配DELETE WHERE条件但不匹配ON条件的行不会被从表中删除. 

185. 

186.下面例子验证DELETE子句. 我们从表NEWPRODUCTS中合并行到表PRODUCTS中, 但删除category为ELECTRNCS的行. 

187. 

188.SQL> MERGE INTO products p 

189. 2 USING newproducts np 

190. 3 ON (p.product_id = np.product_id) 

191. 4 WHEN MATCHED THEN 

192. 5 UPDATE 

193. 6 SET p.product_name = np.product_name, 

194. 7 p.category = np.category 

195. 8 DELETE WHERE (p.category = 'ELECTRNCS') 

196. 9 WHEN NOT MATCHED THEN 

197. 10 INSERT 

198. 11 VALUES (np.product_id, np.product_name, np.category) 

199. SQL> / 

200. 

201. 4 rows merged. 

202. 

203. SQL> SELECT * FROM products; 

204. 

205. PRODUCT_ID PRODUCT_NAME CATEGORY 

206. ---------- -------------------- ---------- 

207. 1501 VIVITAR 35MM ELECTRNCS 

208. 1600 PLAY GYM TOYS 

209. 1601 LAMAZE TOYS 

210. 1666 HARRY POTTER TOYS 

211. 1700 WAIT INTERFACE BOOKS 

212. SQL> 

213. 

214.产品ID为1502的行从表PRODUCTS中被删除, 因为它同时匹配ON条件和DELETE WHERE条件. 产品ID为1501的行匹配DELETE WHERE条件但不匹配ON条件, 所以它没有被删除. 产品ID为1700 的行不匹配ON条件, 所以被插入表PRODUCTS. 产品ID为1601和1666的行匹配ON条件但不匹配DELETE WHERE条件, 所以被更新为表NEWPRODUCTS中的值.