- IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'RoleMenu_ADD')
- BEGIN
- PRINT 'Dropping Procedure RoleMenu_ADD'
- DROP Procedure RoleMenu_ADD
- END
- GO
- PRINT 'Creating Procedure RoleMenu_ADD'
- GO
- CREATE Procedure RoleMenu_ADD
- /* Param List */
- @RoleID int ,
- @MenuID varchar(5000)
- AS
- begin
- --declare @RoleID int, @MenuID varchar(5000)
- --set @MenuID=',12,13,14,15'
- --set @RoleID=5
- create table #allid(allid int)
- --select * from #allID
- create table #familyTree(theid int)
- declare @ids varchar(4000),@str varchar(4000)
- set @ids=@MenuID--得到ID字符串
- set @str=@ids
- set @str=left(@str,len(@str)-1)
- set @str = replace(@str, ',', '''a union all select ''')
- exec ('insert #Familytree(theid) select '''+@str+'''')
- DECLARE @id int,@id2 int,@id3 int
- DECLARE mycursor2 CURSOR FOR
- select theid from #Familytree
- OPEN mycursor2
- FETCH NEXT FROM mycursor2
- INTO @id
- WHILE @@FETCH_STATUS = 0
- BEGIN
- select @id2=MenuParentId from adminMenu where AMID=@id
- insert #allid(allid) select @id
- --select * from adminMenu
- while @id2>0
- begin
- insert #allid(allid) select @id2
- Select @id3=MenuParentId from adminMenu where AMID = @id2
- set @id2=@id3
- continue
- end
- FETCH NEXT FROM mycursor2
- INTO @id
- END
- CLOSE mycursor2
- DEALLOCATE mycursor2
- drop table #FamilyTree
- DECLARE @allidstr varchar(4000),@id1 int
- set @allidstr=''
- DECLARE mycursor1 CURSOR FOR
- select distinct(allid) from #allid order by allid
- OPEN mycursor1
- FETCH NEXT FROM mycursor1
- INTO @id1
- WHILE @@FETCH_STATUS = 0
- BEGIN
- set @allidstr=@allidstr+convert(varchar(10),@id1)+','
- FETCH NEXT FROM mycursor1
- INTO @id1
- END
- CLOSE mycursor1
- DEALLOCATE mycursor1
- drop table #allid
- create table #table2
- (
- theID int
- )
- --select * from #table2
- declare @str22 varchar(5000)
- set @str22=@allidstr
- --set @str=left(@str,len(@str)-1)
- set @str22 = replace(@str22, ',', '''a union all select ''')
- exec ('insert #table2(theid) select '''+@str22+'''')
- BEGIN TRANSACTION--开始事务
- DECLARE @errorSun INT --定义错误计数器
- SET @errorSun=0 --没错为0
- DECLARE @id111 int
- --set @allidstr=''
- DECLARE mycursor111 CURSOR FOR
- select distinct(theID) from #table2 order by theID
- OPEN mycursor111
- FETCH NEXT FROM mycursor111
- INTO @id111
- WHILE @@FETCH_STATUS = 0
- BEGIN
- --set @allidstr=@allidstr+convert(varchar(10),@id1)+','
- insert into roleMenu values(@RoleID,@id111)
- SET @errorSun=@errorSun+@@ERROR --累计是否有错
- FETCH NEXT FROM mycursor111
- INTO @id111
- END
- CLOSE mycursor111
- DEALLOCATE mycursor111
- IF @errorSun<>0
- BEGIN
- --PRINT '有错误,回滚'
- ROLLBACK TRANSACTION--事务回滚语句
- END
- ELSE
- BEGIN
- --PRINT '成功,提交'
- COMMIT TRANSACTION--事务提交语句
- END
- --select * from #table2
- drop table #table2
- end
- GO