IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'dbo.CP_Seg_Menu_Mod_Ordenar') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE dbo.CP_Seg_Menu_Mod_Ordenar 
END
GO

CREATE PROCEDURE dbo.CP_Seg_Menu_Mod_Ordenar @Id_Modulo INT
AS
BEGIN
SET NOCOUNT ON


SELECT ME.*
	INTO #z_Seg_Menu_Mod
			FROM 	dbo.Seg_Menu D, 
					dbo.Seg_Menu_Mod ME
			WHERE 
			D.Id_Menu = ME.Id_Menu 
			AND ME.Id_Modulo = @Id_Modulo
			AND D.Id_Menu <> 0 
			ORDER BY ME.Orden



ALTER TABLE #z_Seg_Menu_Mod ADD _Orden INT IDENTITY(100,10)


UPDATE Seg_Menu_Mod SET Orden = S2._Orden FROM Seg_Menu_Mod S1,#z_Seg_Menu_Mod S2
	WHERE S1.Id_Menu = S2.Id_Menu AND S1.Id_Modulo = S2.Id_Modulo
	AND S1.Orden = S2.Orden
DROP TABLE #z_Seg_Menu_Mod

END

/*
exec CLEVER.DBO.CP_Seg_Menu_Mod_Ordenar 1

*/
