IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_extres1]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_extres1]
GO
-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mps_new_extres1]
-- Add the parameters for the stored procedure here
@cmpid int,
@etc_tag int,
@etc_type nvarchar(100),
@etc_level int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    -- Insert statements for procedure here
DECLARE @id int
DECLARE @order int
DECLARE @caption nvarchar(100)
SELECT TOP 1 @caption=etc_caption FROM mps_ext_resource WHERE etc_cmpid=@cmpid AND etc_tag = @etc_tag ORDER BY etc_order DESC,etc_level DESC
SELECT TOP 1 @order=etc_order FROM mps_ext_resource WHERE etc_cmpid = @cmpid ORDER BY etc_order DESC,etc_level DESC,etc_tag DESC
SET @order = ISNULL(@order+1,0)
print @order
SELECT TOP 1 @id=etc_id FROM mps_ext_resource WHERE etc_cmpid=@cmpid AND etc_type=@etc_type AND etc_tag=@etc_tag
SET @id = ISNULL(@id , 0)
IF(@id = 0)
BEGIN
INSERT INTO mps_ext_resource(etc_cmpid, etc_tag, etc_caption, etc_type, etc_full, etc_order, etc_level)
VALUES(@cmpid, @etc_tag, @caption, @etc_type, @etc_type, @order, @etc_level)
SELECT TOP 1 @id=etc_id FROM mps_ext_resource WHERE etc_cmpid=@cmpid AND etc_type=@etc_type AND etc_tag=@etc_tag
SELECT *,etc_tag as datagroup FROM mps_ext_resource WHERE etc_id=@id
END ELSE
BEGIN
SELECT 0 NONE WHERE 0=1
END
END