从dindan表里更新client表.sql 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- =============================================
  2. -- 程序编写: Jeff
  3. -- 版 本: V 1.0
  4. -- 建立日期: 2015-05-19
  5. -- 功能说明: 从dindan表里更新client表;
  6. -- 备 注:
  7. -- 修改日期:
  8. -- 修改说明:
  9. -- =============================================
  10. use db
  11. declare @order nvarchar(36) -- 订单号;
  12. declare @name1 nvarchar(24) -- 姓名1;
  13. declare @name2 nvarchar(24) -- 姓名2;
  14. declare @phone1 nvarchar(12) -- 手机1;
  15. declare @phone2 nvarchar(12) -- 手机2;
  16. declare @time3 nvarchar(16) -- 婚庆日期;
  17. declare @curIndex int -- 当前索引;
  18. declare @count int -- 查找数量;
  19. -- 将dindan表重新生成到新表里;
  20. select identity(int,1,1) as autoid,* into dindan_temp from dindan
  21. -- 设置curIndex索引的最大值;
  22. select @curIndex = max(autoid) from dindan_temp
  23. -- 根据新表的autoid来逐步更新client数据;
  24. while(@curIndex > 0)
  25. begin
  26. select @order = id, @name1 = name1, @name2 = name2, @phone1 = phone1, @phone2 = phone2, @time3 = time3
  27. from dindan_temp where autoid = @curIndex
  28. print @order
  29. print @name1
  30. print @name2
  31. print @phone1
  32. print @phone2
  33. print @time3
  34. select @count = count(*) from client where id = @order
  35. if( @count > 0 )
  36. update client set name1 = @name1, name2 = @name2, phone1 = @phone1, phone2 = @phone2,time3 = @time3 where id = @order
  37. else
  38. insert client(id,name1,name2,phone1,phone2,time3) values (@order,@name1,@name2,@phone1,@phone2,@time3)
  39. set @curIndex = @curIndex - 1
  40. end
  41. -- 最后,删除临时表;
  42. drop table dindan_temp
  43. /*
  44. declare @order nvarchar(36) -- 订单号;
  45. declare @name1 nvarchar(24) -- 姓名1;
  46. declare @name2 nvarchar(24) -- 姓名2;
  47. declare @phone1 nvarchar(12) -- 手机1;
  48. declare @phone2 nvarchar(12) -- 手机2;
  49. --declare @birthday1 nvarchar(16) -- 生日1;
  50. --declare @birthday2 nvarchar(16) -- 生日2;
  51. declare @time3 nvarchar(16) -- 婚庆日期;
  52. --declare @check1 nvarchar(4) -- 是否农历1;
  53. --declare @check2 nvarchar(4) -- 是否农历2;
  54. --declare @check3 nvarchar(4) -- 是否农历3;
  55. --declare @qq1 nvarchar(16) -- QQ1;
  56. --declare @qq2 nvarchar(16) -- QQ2;
  57. --declare @adr1 nvarchar(64) -- 地址1;
  58. --declare @adr2 nvarchar(64) -- 地址2;
  59. --declare @sex nvarchar(4) -- 性别;
  60. --declare @zodiac nvarchar(4) -- 生肖;
  61. declare @curIndex int -- 当前索引;
  62. declare @count int -- 查找数量;
  63. -- 将dindan表重新生成到新表里;
  64. select identity(int,1,1) as autoid,* into dindan_temp from dindan
  65. -- 设置curIndex索引的最大值;
  66. select @curIndex = max(autoid) from dindan_temp
  67. -- 根据新表的autoid来逐步更新client数据;
  68. while(@curIndex > 0)
  69. begin
  70. select @order = id, @name1 = name1, @name2 = name2, @phone1 = phone1, @phone2 = phone2, @time3 = time3
  71. from dindan_temp where autoid = @curIndex
  72. select @count = count(*) from client where id = @order
  73. if( @count > 0 )
  74. update client set name1 = @name1, name2 = @name2, @phone1 = phone1, @phone2 = phone2,@time3 = time3 where id = @order
  75. else
  76. insert client(id,name1,name2,phone1,phone2,time3) values (@order,@name1,@name2,@phone1,@phone2,@time3)
  77. set @curIndex = @curIndex - 1
  78. end
  79. -- 最后,删除临时表;
  80. drop table dindan_temp
  81. */