IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_insert_Party_for_Form_mlp_stg]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_insert_Party_for_Form_mlp_stg]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[sp_insert_Party_for_Form_mlp_stg]
(@partition_id int)
--,@database_name VARCHAR(20))
AS
BEGIN
BEGIN TRY
SET NOCOUNT ON;
DECLARE @SQL NVARCHAR(4000) =''
DECLARE @SQL_INSERT NVARCHAR(4000)=''
DECLARE @database_name VARCHAR(20)
SELECT @database_name=CASE WHEN @partition_id=1 THEN 'Multi_Line_Policy'
WHEN @partition_id=2 THEN 'Multi_Line_Policy_2'
WHEN @partition_id=3 THEN 'Multi_Line_Policy_3'
WHEN @partition_id=6 THEN 'Multi_Line_Policy_6'
END
SET @SQL=@SQL+'DELETE FROM '+@database_name+'.dbo.[Party] WHERE EXISTS(SELECT 1 FROM
tempdb.dbo.tmp_Party_STG WHERE Party_id=[Party].Party_id)'
--PRINT @SQL
EXEC (@SQL)
SET @SQL_INSERT=@SQL_INSERT+'SET IDENTITY_INSERT '+@database_name+'.dbo.[Party] ON
INSERT INTO '+@database_name+'.dbo.[Party]( party_id
,party_type_id
,created_date
,created_user
,modified_date
,modified_user
,valid_flag
)
SELECT party_id
,party_type_id
,created_date
,created_user
,modified_date
,modified_user
,valid_flag
FROM tempdb.dbo.tmp_Party_STG WITH(NOLOCK)
SET IDENTITY_INSERT '+@database_name+'.dbo.[Party] OFF'
--print @SQL_INSERT
EXEC (@SQL_INSERT)
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(MAX),@ErrorNumber INT,@ErrorSeverity INT,@ErrorState INT
SET @ErrorMessage = ERROR_MESSAGE()
SET @ErrorNumber = ERROR_NUMBER()
SET @ErrorSeverity = ERROR_SEVERITY()
SET @ErrorState = ERROR_STATE()
RAISERROR( @ErrorMessage,@ErrorSeverity,@ErrorState );
END CATCH
END
No comments:
Post a Comment