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

  1. IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'RoleMenu_ADD')
  2.     BEGIN
  3.         PRINT 'Dropping Procedure RoleMenu_ADD'
  4.         DROP  Procedure  RoleMenu_ADD
  5.     END
  6. GO
  7. PRINT 'Creating Procedure RoleMenu_ADD'
  8. GO
  9. CREATE Procedure RoleMenu_ADD
  10.     /* Param List */
  11.     @RoleID int ,
  12.     @MenuID varchar(5000) 
  13. AS
  14. begin
  15.     
  16.   --declare @RoleID int, @MenuID varchar(5000)
  17.   --set @MenuID=',12,13,14,15' 
  18.   --set @RoleID=5
  19.   create table #allid(allid int)
  20.     --select * from #allID
  21.   create table #familyTree(theid int)
  22.   declare @ids varchar(4000),@str varchar(4000)
  23.   set @ids=@MenuID--得到ID字符串
  24.   set @str=@ids
  25.   set @str=left(@str,len(@str)-1)
  26.   set @str = replace(@str, ',''''a union all select ''')
  27.   exec ('insert #Familytree(theid) select '''+@str+'''')
  28.   DECLARE @id int,@id2 int,@id3 int
  29.   DECLARE mycursor2 CURSOR FOR
  30.   select theid from #Familytree
  31.        OPEN mycursor2
  32.        FETCH NEXT FROM mycursor2
  33.        INTO @id
  34.        WHILE @@FETCH_STATUS = 0
  35.        BEGIN
  36.   select @id2=MenuParentId from adminMenu where AMID=@id     
  37.   insert #allid(allid) select @id
  38. --select * from adminMenu
  39.   while @id2>0
  40.   begin
  41.   insert #allid(allid) select @id2
  42.   Select @id3=MenuParentId from adminMenu where AMID = @id2
  43.   set @id2=@id3
  44.   continue
  45.   end 
  46.     
  47.        FETCH NEXT FROM mycursor2
  48.        INTO @id
  49.        END
  50.        CLOSE mycursor2
  51.   DEALLOCATE mycursor2
  52.   drop table #FamilyTree
  53.   DECLARE @allidstr varchar(4000),@id1 int
  54.   set @allidstr=''
  55.   DECLARE mycursor1 CURSOR FOR
  56.   select distinct(allid) from #allid order by allid
  57.        OPEN mycursor1
  58.        FETCH NEXT FROM mycursor1
  59.        INTO @id1
  60.        WHILE @@FETCH_STATUS = 0
  61.        BEGIN
  62.        set @allidstr=@allidstr+convert(varchar(10),@id1)+','
  63.        FETCH NEXT FROM mycursor1
  64.        INTO @id1
  65.        END
  66.        CLOSE mycursor1
  67.   DEALLOCATE mycursor1
  68.     drop table #allid
  69.     
  70. create table #table2
  71.     (
  72.          theID int
  73.     )
  74.     --select * from #table2
  75.     declare @str22 varchar(5000)
  76.     set @str22=@allidstr
  77.     --set @str=left(@str,len(@str)-1)
  78.     set @str22 = replace(@str22, ',''''a union all select ''')
  79.     exec ('insert #table2(theid) select '''+@str22+'''')
  80.     
  81.     BEGIN TRANSACTION--开始事务
  82.     DECLARE @errorSun INT --定义错误计数器
  83.     SET @errorSun=0 --没错为0
  84.     DECLARE @id111 int
  85.     --set @allidstr=''
  86.     DECLARE mycursor111 CURSOR FOR
  87.         select distinct(theID) from #table2 order by theID
  88.         OPEN mycursor111
  89.         FETCH NEXT FROM mycursor111
  90.         INTO @id111
  91.         WHILE @@FETCH_STATUS = 0
  92.         BEGIN
  93.        --set @allidstr=@allidstr+convert(varchar(10),@id1)+','
  94.        insert into roleMenu values(@RoleID,@id111)
  95.        SET @errorSun=@errorSun+@@ERROR --累计是否有错
  96.        
  97.        FETCH NEXT FROM mycursor111
  98.        INTO @id111
  99.        END
  100.        CLOSE mycursor111
  101.        DEALLOCATE mycursor111
  102.     
  103.     IF @errorSun<>0
  104.     BEGIN
  105.         --PRINT '有错误,回滚'
  106.         ROLLBACK TRANSACTION--事务回滚语句
  107.     END
  108.         ELSE
  109.     BEGIN
  110.         --PRINT '成功,提交'
  111.         COMMIT TRANSACTION--事务提交语句
  112.     END
  113.     --select * from #table2
  114.     drop table #table2
  115.     end
  116.     
  117. GO
posted on 2008-11-06 13:26  叮叮猫的编程世界  阅读(418)  评论(0编辑  收藏  举报