Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a user defined stored procedure template #11

Open
jwk6 opened this issue Aug 6, 2020 · 2 comments
Open

Allow a user defined stored procedure template #11

jwk6 opened this issue Aug 6, 2020 · 2 comments

Comments

@jwk6
Copy link

jwk6 commented Aug 6, 2020

Allow a user defined stored procedure template so that the developer can inject other T-SQL code around the merge statement.

For example, I use Azure Data Factory to call a stored procedure like this:

``

--Description:    Merges changes to the [[TableName]] table
--Author:         jwk6
--Create Date:    08/06/2020
--Changed By:  
CREATE PROCEDURE [dbo].[usp_[[TableName]]_Merge]
    @ExecutionID NVARCHAR(90)
AS
BEGIN

    DECLARE @CurrentDateTime DATETIME = GETUTCDATE()
    DECLARE @InsertOperation NVARCHAR(1) = N'I'
    DECLARE @UpdateOperation NVARCHAR(1) = N'U'
    DECLARE @InsertCount INT = 0
    DECLARE @UpdateCount INT = 0
    DECLARE @DeleteCount INT = 0

    DECLARE @Changes TABLE(
        [Operation] NVARCHAR(10));

    DECLARE @ChangeSummary TABLE(
        [Operation] NVARCHAR(10),
        [RecordCount] INT);

    /*body of the merge statement goes here */
    OUTPUT $action into @Changes;

    INSERT INTO @ChangeSummary (
            [Operation], 
            [RecordCount])
    SELECT  Operation, COUNT(*) AS OperationCount
    FROM	@Changes
    GROUP BY Operation;
    
    SET @InsertCount = (SELECT [RecordCount] FROM @ChangeSummary WHERE [Operation] = 'INSERT');
    SET @UpdateCount = (SELECT [RecordCount] FROM @ChangeSummary WHERE [Operation] = 'UPDATE');
    SET @DeleteCount = (SELECT [RecordCount] FROM @ChangeSummary WHERE [Operation] = 'DELETE');

    EXEC usp_Audit_Update '[[TableName]]',
                        @ExecutionID,
                        @InsertCount,
                        @UpdateCount,
                        @DeleteCount

END
GO
@NowinskiK
Copy link
Member

@jwk6, do you want to do this?

@jwk6
Copy link
Author

jwk6 commented Jun 1, 2021

@NowinskiK Yeah, sure! I think I'll have some time and I'm feeling the urge again. ;-)

@NowinskiK NowinskiK modified the milestones: ver.4.3, ver.4.4 Aug 31, 2021
@NowinskiK NowinskiK modified the milestones: ver.4.4, ver.5.0, ver.5.1 Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants