IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_extres]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_extres]
GO
-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mps_new_extres]
-- Add the parameters for the stored procedure here
@cmpid int,
@etc_caption nvarchar(50)
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 @tag int
SELECT TOP 1 @tag=etc_tag FROM mps_ext_resource GROUP BY etc_tag ORDER BY etc_tag DESC 
SET @tag = ISNULL(@tag+1,1)
SELECT TOP 1 @id=etc_id FROM mps_ext_resource WHERE etc_cmpid=@cmpid AND etc_caption=@etc_caption
SET @id = ISNULL(@id , 0)
IF(@id = 0)
BEGIN
INSERT INTO mps_ext_resource(etc_cmpid, etc_tag, etc_caption, etc_full, etc_type, etc_order, etc_level)
VALUES(@cmpid, @tag, @etc_caption, 'new content', 'new content', 0, 0)
SELECT TOP 1 @id=etc_id FROM mps_ext_resource WHERE etc_cmpid=@cmpid AND etc_caption=@etc_caption
SELECT * FROM mps_ext_resource WHERE etc_id=@id
END ELSE
BEGIN
SELECT 0 NONE WHERE 0=1
END
END