IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_docctg]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_docctg]
GO
CREATE procedure [dbo].[mps_new_docctg]
-- Add the parameters for the stored procedure here
@cmpid int,
@dc_caption nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
DECLARE @id int
DECLARE @tag int
SELECT TOP 1 @tag=dc_tag FROM mps_docctg GROUP BY dc_tag ORDER BY dc_tag DESC 
SET @tag = ISNULL(@tag+1,0)
SELECT TOP 1 @id=dc_id FROM mps_docctg WHERE dc_cmpid=@cmpid AND dc_caption=@dc_caption
SET @id = ISNULL(@id , 0)
IF(@id = 0)
BEGIN
INSERT INTO mps_docctg(dc_cmpid, dc_tag, dc_caption, dc_type, dc_order, dc_level)
VALUES(@cmpid, @tag, @dc_caption, 'new content', 0, 0)
SELECT TOP 1 @id=dc_id FROM mps_docctg WHERE dc_cmpid=@cmpid AND dc_caption=@dc_caption
SELECT * FROM mps_docctg WHERE dc_id=@id
END ELSE
BEGIN
SELECT 0 NONE WHERE 0=1
END
set nocount off
END