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

-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mps_new_prdctg]
-- Add the parameters for the stored procedure here
@cmpid int,
@pc_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=pc_tag FROM mps_prdctg GROUP BY pc_tag ORDER BY pc_tag DESC 
SET @tag = ISNULL(@tag+1,1)
SELECT TOP 1 @id=pc_id FROM mps_prdctg WHERE pc_cmpid=@cmpid AND pc_caption=@pc_caption
SET @id = ISNULL(@id , 0)
IF(@id = 0)
BEGIN
INSERT INTO mps_prdctg(pc_cmpid, pc_tag, pc_caption, pc_full, pc_type, pc_order, pc_level)
VALUES(@cmpid, @tag, @pc_caption, 'new content', 'new content', 0, 0)
SELECT TOP 1 @id=pc_id FROM mps_prdctg WHERE pc_cmpid=@cmpid AND pc_caption=@pc_caption
SELECT * FROM mps_prdctg WHERE pc_id=@id
END ELSE
BEGIN
SELECT 0 NONE WHERE 0=1
END
END