一个很复杂SQL存储过程的编写

  1. IFEXISTS(SELECT*FROMsysobjectsWHEREtype='P'ANDname='RoleMenu_ADD')
  2. BEGIN
  3. PRINT'DroppingProcedureRoleMenu_ADD'
  4. DROPProcedureRoleMenu_ADD
  5. END
  6. GO
  7. PRINT'CreatingProcedureRoleMenu_ADD'
  8. GO
  9. CREATEProcedureRoleMenu_ADD
  10. /*ParamList*/
  11. @RoleIDint,
  12. @MenuIDvarchar(5000)
  13. AS
  14. begin
  15. --declare@RoleIDint,@MenuIDvarchar(5000)
  16. --set@MenuID=',12,13,14,15'
  17. --set@RoleID=5
  18. createtable#allid(allidint)
  19. --select*from#allID
  20. createtable#familyTree(theidint)
  21. declare@idsvarchar(4000),@strvarchar(4000)
  22. set@ids=@MenuID--得到ID字符串
  23. set@str=@ids
  24. set@str=left(@str,len(@str)-1)
  25. set@str=replace(@str,',','''aunionallselect''')
  26. exec('insert#Familytree(theid)select'''+@str+'''')
  27. DECLARE@idint,@id2int,@id3int
  28. DECLAREmycursor2CURSORFOR
  29. selecttheidfrom#Familytree
  30. OPENmycursor2
  31. FETCHNEXTFROMmycursor2
  32. INTO@id
  33. WHILE@@FETCH_STATUS=0
  34. BEGIN
  35. select@id2=MenuParentIdfromadminMenuwhereAMID=@id
  36. insert#allid(allid)select@id
  37. --select*fromadminMenu
  38. while@id2>0
  39. begin
  40. insert#allid(allid)select@id2
  41. Select@id3=MenuParentIdfromadminMenuwhereAMID=@id2
  42. set@id2=@id3
  43. continue
  44. end
  45. FETCHNEXTFROMmycursor2
  46. INTO@id
  47. END
  48. CLOSEmycursor2
  49. DEALLOCATEmycursor2
  50. droptable#FamilyTree
  51. DECLARE@allidstrvarchar(4000),@id1int
  52. set@allidstr=''
  53. DECLAREmycursor1CURSORFOR
  54. selectdistinct(allid)from#allidorderbyallid
  55. OPENmycursor1
  56. FETCHNEXTFROMmycursor1
  57. INTO@id1
  58. WHILE@@FETCH_STATUS=0
  59. BEGIN
  60. set@allidstr=@allidstr+convert(varchar(10),@id1)+','
  61. FETCHNEXTFROMmycursor1
  62. INTO@id1
  63. END
  64. CLOSEmycursor1
  65. DEALLOCATEmycursor1
  66. droptable#allid
  67. createtable#table2
  68. (
  69. theIDint
  70. )
  71. --select*from#table2
  72. declare@str22varchar(5000)
  73. set@str22=@allidstr
  74. --set@str=left(@str,len(@str)-1)
  75. set@str22=replace(@str22,',','''aunionallselect''')
  76. exec('insert#table2(theid)select'''+@str22+'''')
  77. BEGINTRANSACTION--开始事务
  78. DECLARE@errorSunINT--定义错误计数器
  79. SET@errorSun=0--没错为0
  80. DECLARE@id111int
  81. --set@allidstr=''
  82. DECLAREmycursor111CURSORFOR
  83. selectdistinct(theID)from#table2orderbytheID
  84. OPENmycursor111
  85. FETCHNEXTFROMmycursor111
  86. INTO@id111
  87. WHILE@@FETCH_STATUS=0
  88. BEGIN
  89. --set@allidstr=@allidstr+convert(varchar(10),@id1)+','
  90. insertintoroleMenuvalues(@RoleID,@id111)
  91. SET@errorSun=@errorSun+@@ERROR--累计是否有错
  92. FETCHNEXTFROMmycursor111
  93. INTO@id111
  94. END
  95. CLOSEmycursor111
  96. DEALLOCATEmycursor111
  97. IF@errorSun<>0
  98. BEGIN
  99. --PRINT'有错误,回滚'
  100. ROLLBACKTRANSACTION--事务回滚语句
  101. END
  102. ELSE
  103. BEGIN
  104. --PRINT'成功,提交'
  105. COMMITTRANSACTION--事务提交语句
  106. END
  107. --select*from#table2
  108. droptable#table2
  109. end
  110. GO
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值