复制renyuan表和user表到其他数据库.sql 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. -- =============================================
  2. -- 程序编写: Jeff
  3. -- 版 本: V 1.0
  4. -- 建立日期: 2015-05-20
  5. -- 功能说明: 复制renyuan表和user表到其他数据库;
  6. -- 备 注:
  7. -- 修改日期:
  8. -- 修改说明:
  9. -- =============================================
  10. -- 创建临时数据库,用于存储需要的表;
  11. create database temp
  12. on primary -- 默认就属于primary文件组,可省略
  13. (
  14. /*--数据文件的具体描述--*/
  15. name = 'temp', -- 主数据文件的逻辑名称
  16. filename = 'D:\temp.mdf', -- 主数据文件的物理名称
  17. size = 2mb, -- 主数据文件的初始大小
  18. maxsize = 100mb, -- 主数据文件增长的最大值
  19. filegrowth = 15% -- 主数据文件的增长率
  20. )
  21. log on
  22. (
  23. /*--日志文件的具体描述,各参数含义同上--*/
  24. name = 'temp_log',
  25. filename = 'D:\temp_log.ldf',
  26. size = 2mb,
  27. filegrowth = 1mb
  28. )
  29. go
  30. -- 将数据插入到临时数据库中;
  31. select identity(int,1,1) as autoid,* into [temp].[dbo].[renyuan] from [ddf].[dbo].[renyuan]
  32. select identity(int,1,1) as autoid,* into [temp].[dbo].[user] from [ddf].[dbo].[user]
  33. go
  34. -- 将临时数据库附加到外网分店中,重新将临时数据库的表数据转移出来;
  35. -- user表,以account和name唯一;
  36. -- renyuan表,以name和id唯一;
  37. ------------------------------------------------------------------
  38. declare @r_id nvarchar(24) -- 员工表id;
  39. declare @r_name nvarchar(24) -- 员工表姓名;
  40. declare @r_bm nvarchar(24) -- 员工表部门;
  41. declare @r_sex nvarchar(24) -- 员工表;
  42. declare @r_phone nvarchar(24) -- 员工表;
  43. declare @r_address nvarchar(50) -- 员工表;
  44. declare @r_bz nvarchar(max) -- 员工表;
  45. declare @r_base nvarchar(24) -- 员工表;
  46. declare @r_rate1 nvarchar(24) -- 员工表;
  47. declare @r_rate2 nvarchar(24) -- 员工表;
  48. declare @r_bz2 nvarchar(max) -- 员工表;
  49. declare @r_address2 nvarchar(50) -- 员工表;
  50. declare @r_idno nvarchar(50) -- 员工表;
  51. declare @r_marry nvarchar(24) -- 员工表;
  52. declare @r_dimission nvarchar(24) -- 员工表;
  53. declare @r_rate3 nvarchar(24) -- 员工表;
  54. declare @r_rate4 nvarchar(24) -- 员工表;
  55. declare @r_rate5 nvarchar(24) -- 员工表;
  56. declare @r_loginno1 nvarchar(24) -- 员工表;
  57. declare @r_loginno2 nvarchar(24) -- 员工表;
  58. --declare @r_photo image -- 员工表;
  59. declare @r_level nvarchar(24) -- 员工表;
  60. ----------------------------------------------------------------
  61. declare @Index int -- 索引值;
  62. declare @count int -- 数量;
  63. -- 先处理renyuan表;
  64. select @Index = count(*) from [temp].[dbo].[renyuan]
  65. while(@Index > 0)
  66. begin
  67. select @r_id = id,
  68. @r_name = [name],
  69. @r_bm = bm,
  70. @r_sex = sex,
  71. @r_phone = phone,
  72. @r_address = [address],
  73. @r_bz=bz,
  74. @r_base = base,
  75. @r_rate1 = rate1,
  76. @r_rate2 = rate2,
  77. @r_bz2 = bz2,
  78. @r_address2 = address2,
  79. @r_idno = idno,
  80. @r_marry = marry,
  81. @r_dimission = dimission,
  82. @r_rate3 = rate3,
  83. @r_rate4 = rate4,
  84. @r_rate5 = rate5,
  85. @r_loginno1 = loginno1,
  86. @r_loginno2 = loginno2,
  87. @r_level = [level]
  88. from [temp].[dbo].[renyuan] where autoid = @Index
  89. select @count = count(*) from [ddf].[dbo].[renyuan] where id = @r_id and [name] = @r_name
  90. if( @count = 0 )
  91. insert into [ddf].[dbo].[renyuan]
  92. ([bm]
  93. ,[id]
  94. ,[name]
  95. ,[sex]
  96. ,[phone]
  97. ,[address]
  98. ,[bz]
  99. ,[base]
  100. ,[rate1]
  101. ,[rate2]
  102. ,[bz2]
  103. ,[address2]
  104. ,[idno]
  105. ,[marry]
  106. ,[dimission]
  107. ,[rate3]
  108. ,[rate4]
  109. ,[rate5]
  110. ,[loginno1]
  111. ,[loginno2]
  112. ,[level])values
  113. (@r_bm,@r_id,@r_name,@r_sex,@r_phone,@r_address,@r_bz,@r_base,
  114. @r_rate1,@r_rate2,@r_bz2,@r_address2,@r_idno,@r_marry,@r_dimission,
  115. @r_rate3,@r_rate4,@r_rate5,@r_loginno1,@r_loginno2,@r_level)
  116. print @count
  117. set @Index = @Index - 1
  118. end
  119. go
  120. ------------------------------------------------------------------
  121. declare @u_account nvarchar(24) -- 用户表账号;
  122. declare @u_name nvarchar(24) -- 用户表姓名;
  123. declare @u_psw nvarchar(50) -- 用户表;
  124. declare @u_rights nvarchar(50) -- 用户表;
  125. declare @u_rate1 nvarchar(50) -- 用户表;
  126. declare @u_rate2 nvarchar(50) -- 用户表;
  127. declare @u_base nvarchar(50) -- 用户表;
  128. declare @u_bz nvarchar(50) -- 用户表;
  129. declare @u_rights2 nvarchar(50) -- 用户表;
  130. declare @u_discount nvarchar(50) -- 用户表;
  131. declare @u_discount2 nvarchar(50) -- 用户表;
  132. ------------------------------------------------------------------
  133. declare @Index int -- 索引值;
  134. declare @count int -- 数量;
  135. ------------------------------------------------------------------
  136. select @Index = count(*) from [temp].[dbo].[user]
  137. while( @Index > 0)
  138. begin
  139. select
  140. @u_account = account,
  141. @u_name = [name],
  142. @u_psw = psw,
  143. @u_rights = rights,
  144. @u_rate1 = rate1,
  145. @u_rate2 = rate2,
  146. @u_base = base,
  147. @u_bz = bz,
  148. @u_rights2 = rights2,
  149. @u_discount = discount,
  150. @u_discount2 = discount2
  151. from [temp].[dbo].[user] where autoid = @Index
  152. select @count = count(*) from [ddf].[dbo].[user] where account = @u_account and [name] = @u_name
  153. if ( @count = 0 )
  154. insert into [ddf].[dbo].[user]
  155. ([account]
  156. ,[name]
  157. ,[psw]
  158. ,[rights]
  159. ,[rate1]
  160. ,[rate2]
  161. ,[base]
  162. ,[bz]
  163. ,[rights2]
  164. ,[discount]
  165. ,[discount2]) values
  166. (@u_account,@u_name,@u_psw,@u_rights,@u_rate1,@u_rate2,@u_base,@u_bz,@u_rights2,@u_discount,@u_discount2)
  167. print @count
  168. set @Index = @Index - 1
  169. end
  170. go