IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_delete_weixin_state]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_delete_weixin_state]
GO

-- =============================================
-- Author:郑华建
-- Create date: 2017年12月28日 15点39分
-- Description:删除state以及state对应的菜单
-- =============================================
create PROCEDURE [dbo].[mps_delete_weixin_state]
@wxs_id int
AS
BEGIN
SET NOCOUNT ON;

declare @children varchar(100)
set @children=''
--获取要删除项的子项
select @children+=CONVERT(varchar,wxs_id)+',' from mps_weixinstate  where wxs_sub=@wxs_id
if(LEN(@children) > 0)
begin
declare @item varchar(10)
declare @index int
while @children >''
begin
set @index = CHARINDEX(',',@children)
set @item = SUBSTRING(@children,1,@index-1);
set @children = SUBSTRING(@children,@index+1,LEN(@children))
if(@item > '')
begin
exec [mps_delete_weixin_state] @item
end
if(@index=0)
begin
set @children=''
end
end

end
--删除对应导航栏菜单下的子项
delete from mps_menu where mn_sub in  (select mn_id from mps_menu where mn_flag='head_state'+CONVERT(varchar,@wxs_id))
--删除导航栏菜单
delete from mps_menu where mn_flag='head_state'+CONVERT(varchar,@wxs_id)
--删除state
delete from mps_weixinstate where wxs_id = @wxs_id
END