if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_Insert_T_FR_DOCUMENT]') and xtype = N'P')
DROP PROCEDURE  [dbo].[sp_Insert_T_FR_DOCUMENT]
GO

--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- Хранимки для обновления регистра льготников
--Create procedure [dbo].[sp_Insert_T_FR_DOCUMENT] (
--@Snils as varchar(255)
--, @Name as varchar(255)
--, @Number as varchar(255)
--, @Benefits as varchar(255)
--)
--as

--declare @bid uniqueidentifier
--select @bid = BenefitsID from [AKUZ].T_BENEFITS where BenefitCode = @Benefits and BenefitViews = 19001

--if not exists (select 0 from [AKUZ].T_SNILS where Snils = @Snils)
--insert [AKUZ].T_SNILS (Snils) values (@Snils)

--if exists (select 0 from [AKUZ].T_FR_DOCUMENT where Snils = @Snils and Benefits = @bid)
--update [AKUZ].T_FR_DOCUMENT set Name = @Name, Number = @Number where Snils = @Snils and Benefits = @bid
--else
--insert [AKUZ].T_FR_DOCUMENT (FrDocumentID, Name, Number, Benefits, Snils)
--values (NEWID(), @Name, @Number, @bid, @Snils)