Generate MERGE statements with Table data

  • Comments posted to this topic are about the item Generate MERGE statements with Table data

  • Thanks for providing that alternate implementation Celko.

    I can definitely see advantages of your approach, but I have a question: how would one remove extraneous records with your script?

    Say I work with a feline fanatic and she adds a cat to my Zoo. But my Zoo is very exclusive and only the most exotic animals should be allowed to stay inside. What I want is to eliminate any animal records that aren't included in my script, because my script represents the canonical source of my animal list (a sort of "Noah's Ark" to take the analogy one step further :-))

    In the OP MERGE statement, the "IF NOT MATCHED THEN DELETE" clause takes care of removing the extraneous rows.

    Given that a call to the "Zoo_Keeper" proc would be required for each record in your example, do you have any ideas how the DELETE might be achieved?

  • Needs a check for tables without primary keys.

    IF IsNull(@PK_column_list, '') = ''

    BEGIN

    RAISERROR('Table has no primary keys. There should at least be one column in order to have a valid join',16,1)

    RETURN -1 --Failure. Reason: looks like table doesn't have any primary keys

    END

  • Thanks for that John, I've revised the script to include the Primary Key check.

    Unfortunately I'm not able to edit the original article, so have zipped the SQL file here:

  • danere (4/3/2012)


    Thanks for that John, I've revised the script to include the Primary Key check.

    Unfortunately I'm not able to edit the original article, so have zipped the SQL file here:

    You might also consider:

    DECLARE @db varchar(120);

    set @db = 'USE ' + DB_NAME();

    print @db;

    print '';

    PRINT '--MERGE generated by ''sp_generate_merge'' stored procedure, Version 0.9'

    ....

    @Actual_Values nvarchar(max)

    ....

    SET @Actual_Values =

    'SELECT @valuesOUT = IsNull(@valuesOUT, '''') + ' +

    CASE WHEN @top IS NULL OR @top < 0 THEN '' ELSE ' TOP ' + LTRIM(STR(@top)) + ' ' END +

    '''' +

    ' '' + char(10) + CASE WHEN ROW_NUMBER() OVER (ORDER BY ' + @PK_column_list + ') = 1 THEN '' '' ELSE '','' END + ''(''+ ' + @Actual_Values + '+'')''' + ' ' +

    COALESCE(@from,' FROM ' + @Source_Table_Qualified + ' (NOLOCK)')

    ....

    DECLARE @values nvarchar(max);

    DECLARE @ParmDefinition nvarchar(500);

    SET @ParmDefinition = N'@valuesOUT nvarchar(max) OUTPUT';

    EXECUTE sp_executesql @actual_values, @ParmDefinition, @valuesOUT=@values OUTPUT;

    print @values;

    ....

    This skips outputing the results to a table, sets the DB, and goes directly to the messages window, ready to copy and run.

  • One final note, you're comparing the columns using <> which fails to produce a match when either column is NULL. I replaced your single SOURCE.Column <> TARGET.Column check with

    NULLIF(SOURCE.Column, TARGET.Column) IS NOT NULL OR NULLIF(TARGET.Column, SOURCE.Column) IS NOT NULL

    NULLIF will return a NOT null value when the first parameter is non null and the second is parameter is NULL or another value.

  • Thanks John!

    I've made a few changes based on your suggestions and attached v0.92:

    1) The generated script now includes a "USE [DatabaseName]" statement at the start, if the @include_use_db (bit) param is set to 1 (default).

    2) Fixed "has data changed?" check in the WHEN MATCHED clause to correctly test for NULL equality, on both sides (source & target).

    3) Dynamic query string variable (@Actual_Values) expanded to NVARCHAR(MAX) to cope with longer SELECT statements.

    Regarding the PRINT statement enhancement you suggested, I ran into an issue with truncation of result sets over 4000 characters in length. Any ideas how to get around this?

  • danere (4/15/2012)


    Regarding the PRINT statement enhancement you suggested, I ran into an issue with truncation of result sets over 4000 characters in length. Any ideas how to get around this?

    The problem is that PRINT can only print 4000/8000 characters (depending upon whether it is varchar or nvarchar), so the solution depends upon your data.

    If all of the textual representation of the data in a single row will always fit within the limit, it's easy enough to loop over the results, printing out one line at a time. If it can be more than limit then it gets more complicated as print always prints a line at a time, if a single column can be more than the limit, then it's more complicated -- something could be done with varbinary, but I don't have that worked out offhand. Basically varbinary can be easily split and joined together, but I'm not sure about the best general purpose solution in that case (if it's just one column at the end, it shouldn't be too difficult to do)...

  • john.moreno (4/16/2012)

    The problem is that PRINT can only print 4000/8000 characters (depending upon whether it is varchar or nvarchar), so the solution depends upon your data.

    If all of the textual representation of the data in a single row will always fit within the limit, it's easy enough to loop over the results, printing out one line at a time.

    I think for most cases a 4000/8000 character limit per row isn't going to be an issue (none of the tables I have come close to this). I'll take a look at incorporating a loop to print it out each row separately as you suggested.

    Btw I just noticed that there is a "Maximum number of characters displayed in each column" setting in SSMS, which is limited to 8192 characters. Not sure how this might affect the varbinary solution you mentioned (?).

  • danere (4/17/2012)


    john.moreno (4/16/2012)

    The problem is that PRINT can only print 4000/8000 characters (depending upon whether it is varchar or nvarchar), so the solution depends upon your data.

    If all of the textual representation of the data in a single row will always fit within the limit, it's easy enough to loop over the results, printing out one line at a time.

    I think for most cases a 4000/8000 character limit per row isn't going to be an issue (none of the tables I have come close to this). I'll take a look at incorporating a loop to print it out each row separately as you suggested.

    Btw I just noticed that there is a "Maximum number of characters displayed in each column" setting in SSMS, which is limited to 8192 characters. Not sure how this might affect the varbinary solution you mentioned (?).

    It doesn't matter for the varbinary solution as that is using PRINT, what it means is that your original solution would fail when the textual representation of a row is more than 8,192 characters (the row would get truncated). Basically it's slightly more than PRINT solution that does it row by row.

  • I have made a revision to the proc to get around the 8192 character limit of SSMS.

    It uses XML to output the results by default. After executing the proc, simply click on the link within the grid view and copy the MERGE statement out of the XML fragment.

    If you prefer the old "output as text" behaviour, simply specify @results_to_text = 1, however this method is still subject to the truncation problem.

    I'd like to acknowledge Nathan Skerl for providing this solution:

    http://stackoverflow.com/a/10489767/266882

    Happy merging!

  • Very useful. Thanks!

     

  • This script has now been relocated to GitHub. Future updates will be posted there.

    You can always download the latest version from:

    https://github.com/readyroll/generate-sql-merge/archive/master.zip

    More info:

    https://github.com/readyroll/generate-sql-merge

  • What a cool script, thanks.

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply