IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_title_grade]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_title_grade]
GO
CREATE procedure [dbo].[mps_new_title_grade]
-- Add the parameters for the stored procedure here
@cmpid int,
@tg_grade int,
@tg_caption nvarchar(100)
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
SELECT TOP 1 @id=tg_id FROM mps_title_grade WHERE @cmpid=tg_cmpid AND @tg_grade=tg_grade AND @tg_caption=tg_caption
SET @id = ISNULL(@id , 0)
IF(@id = 0)
BEGIN
INSERT INTO mps_title_grade(tg_cmpid, tg_grade, tg_caption)
VALUES(@cmpid, @tg_grade, @tg_caption)
SELECT TOP 1 @id=tg_id FROM mps_title_grade WHERE @cmpid=tg_cmpid AND @tg_grade=tg_grade AND @tg_caption=tg_caption
SELECT * FROM mps_title_grade WHERE tg_id=@id
END ELSE
BEGIN
SELECT 0 NONE WHERE 0=1
END
END