2次处理转历史数据问题.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. --背景:2015年1月1日转2014数据库造成的[db]数据库部分重要数据丢失。
  2. --历史处理过程:
  3. --1.处理过1次,将2014的数据转回db,但后来发现还是有遗失;
  4. --本次为第2次处理;
  5. --转历史数据相关的表如下:
  6. /*
  7. 1.dindan
  8. 2.dindanjd
  9. 3.dindanbukuan
  10. 4.dindanbukuan2
  11. 5.dindansp
  12. 6.dindansp2
  13. 7.dindansp3
  14. 8.client
  15. 9.photoprint
  16. 10.burncdreg
  17. 11.digitalwork
  18. 12.mywork
  19. 13.gudingfeiyong
  20. 14.singleincome
  21. 15.singleincomemoney
  22. 16.client2phonerecord
  23. 17.client2phonerecordout
  24. 18.client3phonerecord
  25. 19.client3phonerecordout
  26. 20.employeephonerecord
  27. 21.employeephonerecordout
  28. 22.phonerecord
  29. 23.phonerecordout
  30. 24.unknownphonerecord
  31. 25.unknownphonerecordout
  32. 26.misscallrecord
  33. 27.sendreg
  34. --其中重要的表是:
  35. 1.dindan
  36. 2.dindanjd
  37. 3.dindanbukuan
  38. 4.dindanbukuan2
  39. 5.dindansp
  40. 6.dindansp2
  41. 7.dindansp3
  42. 8.client
  43. 11.digitalwork
  44. 12.mywork
  45. 13.gudingfeiyong
  46. 14.singleincome
  47. 15.singleincomemoney
  48. */
  49. /*
  50. 将[db]和[2014]中的表数据去掉标识列然后插入到临时表#temp中,再删除原表,然后重新创建原表并distinct插入回原表中
  51. */
  52. --1[dindanbukuan]表;
  53. /*
  54. insert into [db].[dbo].[dindanbukuan#] --([id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck])
  55. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck]
  56. from [db].[dbo].[dindanbukuan]
  57. insert into [db].[dbo].[dindanbukuan#] --([id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck])
  58. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck]
  59. from [2014].[dbo].[dindanbukuan]
  60. */
  61. --drop table [db].[dbo].[dindanbukuan#]
  62. go
  63. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck]
  64. into [db].[dbo].[dindanbukuan#]
  65. from [db].[dbo].[dindanbukuan]
  66. go
  67. insert into [db].[dbo].[dindanbukuan#]
  68. select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck]
  69. from [2014].[dbo].[dindanbukuan]
  70. go
  71. use db
  72. go
  73. select distinct * into #temp from [db].[dbo].[dindanbukuan#]
  74. go
  75. drop table dindanbukuan#
  76. go
  77. drop table dindanbukuan
  78. go
  79. GO
  80. CREATE TABLE [dbo].[dindanbukuan](
  81. [autoid] [int] IDENTITY(1,1) NOT NULL,
  82. [id] [nvarchar](50) NULL,
  83. [money] [nvarchar](50) NULL,
  84. [ren] [nvarchar](50) NULL,
  85. [date] [nvarchar](50) NULL,
  86. [bz] [nvarchar](max) NULL,
  87. [kind] [nvarchar](50) NULL,
  88. [ren2] [nvarchar](50) NULL,
  89. [item] [nvarchar](max) NULL,
  90. [paytype] [nvarchar](50) NULL,
  91. [jdd] [nvarchar](50) NULL,
  92. [financecheck] [nvarchar](10) NULL,
  93. [discount] [nvarchar](50) NULL,
  94. [time] [nvarchar](50) NULL
  95. ) ON [PRIMARY]
  96. GO
  97. go
  98. insert into [db].[dbo].[dindanbukuan] ([id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck]) select [id],[money],[ren],[date],[bz],[kind],[ren2],[item],[paytype],[jdd],[financecheck] from #temp
  99. go
  100. drop table #temp
  101. go
  102. --2[dindanbukuan2]表
  103. select [id],[money],[ren],[date],[bz],[kind],[ren2] into [db].[dbo].[dindanbukuan2#]
  104. from [db].[dbo].[dindanbukuan2]
  105. insert into [db].[dbo].[dindanbukuan2#]
  106. select [id],[money],[ren],[date],[bz],[kind],[ren2]
  107. from [2014].[dbo].[dindanbukuan2]
  108. go
  109. select distinct * into #temp from dindanbukuan2#
  110. go
  111. drop table dindanbukuan2#
  112. drop table dindanbukuan2
  113. GO
  114. CREATE TABLE [dbo].[dindanbukuan2](
  115. [autoid] [int] IDENTITY(1,1) NOT NULL,
  116. [id] [nvarchar](50) NULL,
  117. [money] [nvarchar](50) NULL,
  118. [ren] [nvarchar](50) NULL,
  119. [date] [nvarchar](50) NULL,
  120. [bz] [nvarchar](500) NULL,
  121. [kind] [nvarchar](50) NULL,
  122. [ren2] [nvarchar](50) NULL
  123. ) ON [PRIMARY]
  124. GO
  125. go
  126. insert into [db].[dbo].[dindanbukuan2]([id],[money],[ren],[date],[bz],[kind],[ren2]) select [id],[money],[ren],[date],[bz],[kind],[ren2] from #temp
  127. go
  128. drop table #temp
  129. go
  130. --5[dindansp]
  131. select [id],[spid],[shuliang],[kind],[price]
  132. ,[name],[no],[status1],[status2],[status3]
  133. ,[status4],[date1],[date2],[date3],[date4],[name1]
  134. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  135. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  136. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz]
  137. into [db].[dbo].[dindansp#] from [db].[dbo].[dindansp]
  138. insert into [db].[dbo].[dindansp#]
  139. select [id],[spid],[shuliang],[kind],[price]
  140. ,[name],[no],[status1],[status2],[status3]
  141. ,[status4],[date1],[date2],[date3],[date4],[name1]
  142. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  143. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  144. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz]
  145. from [2014].[dbo].[dindansp]
  146. go
  147. select distinct * into #temp from dindansp#
  148. go
  149. drop table dindansp#
  150. drop table dindansp
  151. GO
  152. CREATE TABLE [dbo].[dindansp](
  153. [id] [nvarchar](50) NULL,
  154. [spid] [nvarchar](50) NULL,
  155. [shuliang] [nvarchar](120) NULL,
  156. [kind] [nvarchar](10) NULL,
  157. [price] [nvarchar](50) NULL,
  158. [name] [nvarchar](50) NULL,
  159. [no] [nvarchar](max) NULL,
  160. [status1] [nvarchar](50) NULL,
  161. [status2] [nvarchar](50) NULL,
  162. [status3] [nvarchar](50) NULL,
  163. [status4] [nvarchar](50) NULL,
  164. [autoid] [int] IDENTITY(1,1) NOT NULL,
  165. [date1] [nvarchar](50) NULL,
  166. [date2] [nvarchar](50) NULL,
  167. [date3] [nvarchar](50) NULL,
  168. [date4] [nvarchar](50) NULL,
  169. [name1] [nvarchar](50) NULL,
  170. [name2] [nvarchar](50) NULL,
  171. [name3] [nvarchar](50) NULL,
  172. [name4] [nvarchar](50) NULL,
  173. [hqdate] [nvarchar](50) NULL,
  174. [hqtime] [nvarchar](50) NULL,
  175. [urgent] [nvarchar](50) NULL,
  176. [taketime] [nvarchar](50) NULL,
  177. [no2] [nvarchar](max) NULL,
  178. [growthtxname] [nvarchar](50) NULL,
  179. [status5] [nvarchar](50) NULL,
  180. [status6] [nvarchar](50) NULL,
  181. [date5] [nvarchar](50) NULL,
  182. [date6] [nvarchar](50) NULL,
  183. [name5] [nvarchar](50) NULL,
  184. [name6] [nvarchar](50) NULL,
  185. [ps] [nvarchar](50) NULL,
  186. [zs] [nvarchar](50) NULL,
  187. [ren1] [nvarchar](120) NULL,
  188. [ren2] [nvarchar](120) NULL,
  189. [hqstatus] [nvarchar](50) NULL,
  190. [bz] [nvarchar](200) NULL
  191. ) ON [PRIMARY]
  192. GO
  193. go
  194. insert into dindansp([id],[spid],[shuliang],[kind],[price]
  195. ,[name],[no],[status1],[status2],[status3]
  196. ,[status4],[date1],[date2],[date3],[date4],[name1]
  197. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  198. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  199. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz] ) select [id],[spid],[shuliang],[kind],[price]
  200. ,[name],[no],[status1],[status2],[status3]
  201. ,[status4],[date1],[date2],[date3],[date4],[name1]
  202. ,[name2],[name3],[name4],[hqdate],[hqtime],[urgent]
  203. ,[taketime],[no2],[status5],[status6],[date5],[date6],[name5]
  204. ,[name6],[growthtxname],[ps],[zs],[ren1],[ren2],[hqstatus],[bz] from #temp
  205. go
  206. drop table #temp
  207. go
  208. --6[dindanjd]
  209. select * into [db].[dbo].[dindanjd#] from [2014].[dbo].[dindanjd]
  210. insert into [db].[dbo].[dindanjd#] select * from [db].[dbo].[dindanjd]
  211. go
  212. select distinct * into #temp from dindanjd#
  213. go
  214. drop table dindanjd#
  215. drop table dindanjd
  216. GO
  217. CREATE TABLE [dbo].[dindanjd](
  218. [id] [nvarchar](50) NULL,
  219. [name] [nvarchar](50) NULL,
  220. [date] [nvarchar](50) NULL,
  221. [time] [nvarchar](50) NULL,
  222. [waiter1] [nvarchar](50) NULL,
  223. [waiter2] [nvarchar](50) NULL,
  224. [status] [nvarchar](50) NULL,
  225. [waiter12] [nvarchar](50) NULL,
  226. [waiter22] [nvarchar](50) NULL,
  227. [bookingdate] [nvarchar](50) NULL,
  228. [dress] [nvarchar](150) NULL,
  229. [bz] [nvarchar](150) NULL,
  230. [clerk] [nvarchar](120) NULL,
  231. [inputtime] [nvarchar](50) NULL,
  232. [branch] [nvarchar](50) NULL
  233. ) ON [PRIMARY]
  234. GO
  235. go
  236. insert into dindanjd select * from #temp
  237. go
  238. drop table #temp
  239. go
  240. --7dindansp2表;
  241. select * into [db].[dbo].[dindansp2#] from [db].[dbo].[dindansp2]
  242. insert into [db].[dbo].[dindansp2#] select * from [2014].[dbo].[dindansp2]
  243. go
  244. select distinct * into #temp from dindansp2#
  245. go
  246. drop table dindansp2#
  247. drop table dindansp2
  248. GO
  249. CREATE TABLE [dbo].[dindansp2](
  250. [id] [nvarchar](50) NULL,
  251. [name] [nvarchar](50) NULL,
  252. [count] [nvarchar](50) NULL,
  253. [date] [nvarchar](50) NULL,
  254. [clerk] [nvarchar](50) NULL
  255. ) ON [PRIMARY]
  256. GO
  257. go
  258. insert into dindansp2 select * from #temp
  259. go
  260. drop table #temp
  261. go
  262. --8dindansp3表;
  263. select * into [db].[dbo].[dindansp3#] from [db].[dbo].[dindansp3]
  264. insert into [db].[dbo].[dindansp3#] select * from [2014].[dbo].[dindansp3]
  265. go
  266. select distinct * into #temp from dindansp3#
  267. go
  268. drop table dindansp3#
  269. drop table dindansp3
  270. GO
  271. CREATE TABLE [dbo].[dindansp3](
  272. [id] [nvarchar](50) NULL,
  273. [spid] [nvarchar](50) NULL,
  274. [name] [nvarchar](50) NULL,
  275. [count] [nvarchar](50) NULL
  276. ) ON [PRIMARY]
  277. GO
  278. go
  279. insert into dindansp3 select * from #temp
  280. go
  281. drop table #temp
  282. go
  283. --9[gudingfeiyong]
  284. select [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[financecheck2],[financecheck3],[hasphoto] into [db].[dbo].[gudingfeiyong#]
  285. from [2014].[dbo].[gudingfeiyong]
  286. insert into [db].[dbo].[gudingfeiyong#]
  287. select [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[financecheck2],[financecheck3],[hasphoto]
  288. from [db].[dbo].[gudingfeiyong]
  289. go
  290. select distinct * into #temp from gudingfeiyong#
  291. go
  292. drop table gudingfeiyong#
  293. drop table gudingfeiyong
  294. GO
  295. CREATE TABLE [dbo].[gudingfeiyong](
  296. [id] [int] IDENTITY(1,1) NOT NULL,
  297. [name] [nvarchar](50) NULL,
  298. [money] [nvarchar](50) NULL,
  299. [dat] [nvarchar](50) NULL,
  300. [bz] [nvarchar](max) NULL,
  301. [renyuan1] [nvarchar](50) NULL,
  302. [renyuan2] [nvarchar](50) NULL,
  303. [financecheck] [nvarchar](10) NULL,
  304. [time] [nvarchar](50) NULL,
  305. [photo] [image] NULL,
  306. [financecheck2] [nvarchar](50) NULL,
  307. [financecheck3] [nvarchar](50) NULL,
  308. [hasphoto] [nvarchar](50) NULL
  309. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  310. GO
  311. go
  312. insert into gudingfeiyong([name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[financecheck2],[financecheck3],[hasphoto]) select [name],[money],[dat],[bz],[renyuan1],[renyuan2],[financecheck],[time],[financecheck2],[financecheck3],[hasphoto] from #temp
  313. go
  314. drop table #temp
  315. go
  316. --10[digitalwork]
  317. select * into [db].[dbo].[digitalwork#] from [db].[dbo].[digitalwork]
  318. insert into [db].[dbo].[digitalwork#] select * from [2014].[dbo].[digitalwork]
  319. go
  320. select distinct * into #temp from digitalwork#
  321. go
  322. drop table digitalwork#
  323. drop table digitalwork
  324. GO
  325. CREATE TABLE [dbo].[digitalwork](
  326. [id] [nvarchar](50) NULL,
  327. [waiter1] [nvarchar](50) NULL,
  328. [waiter2] [nvarchar](50) NULL,
  329. [waiter3] [nvarchar](50) NULL,
  330. [date1] [nvarchar](50) NULL,
  331. [date2] [nvarchar](50) NULL,
  332. [date3] [nvarchar](50) NULL
  333. ) ON [PRIMARY]
  334. GO
  335. go
  336. insert into digitalwork select * from #temp
  337. go
  338. drop table #temp
  339. go
  340. --11[mywork]
  341. select * into [db].[dbo].[mywork#] from [db].[dbo].[mywork]
  342. insert into [db].[dbo].[mywork#] select * from [2014].[dbo].[mywork]
  343. go
  344. select distinct * into #temp from mywork#
  345. go
  346. drop table mywork#
  347. drop table mywork
  348. go
  349. GO
  350. CREATE TABLE [dbo].[mywork](
  351. [id] [nvarchar](50) NULL,
  352. [name] [nvarchar](50) NULL,
  353. [datetime] [nvarchar](50) NULL,
  354. [count1] [nvarchar](50) NULL,
  355. [count2] [nvarchar](50) NULL,
  356. [count3] [nvarchar](50) NULL,
  357. [type] [nvarchar](50) NULL,
  358. [inputdate] [nvarchar](50) NULL,
  359. [inputren] [nvarchar](50) NULL
  360. ) ON [PRIMARY]
  361. GO
  362. insert into mywork select * from #temp
  363. go
  364. drop table #temp
  365. go