sp to move data between matching tables

  • I find myself having to move specific portions of data from a final staging table to a live table on another server pretty often. It's rather tedious writing out the inserts, so I wrote this to do it for me.

    Since the columns are the same, and I just have to move new data, I only have to select the column list from the table I'm moving to. I can also specify a query to go along with it. One thing I always have to do is make sure statusflag = 0; that's for valid records. Hope this helps someone.

    I did see some other SPs that do similar things, but I wanted to try it without a cursor to build the column list.

    USE [Sample]

    GO

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    alter procedure [dbo].[sampleinsert]

    --EXECUTE sampleinsert 'from table', 'to table', 'where reps = 1'

    @tablefrom varchar(512),

    @tableto varchar(512),

    @query varchar(512) = ''

    as

    declare @liststr varchar(max)

    declare @sql varchar(max)

    declare @moveto varchar(max)

    select @liststr = coalesce(@liststr+',' ,'') + column_name

    from information_schema.columns where table_name = '' + @tablefrom + ''

    print (@liststr)

    set @moveto = (select (left(@liststr, len(@liststr))))

    print (@moveto)

    set @sql = 'insert into [tableto]

    ([moveto])

    select

    [moveto]

    from [tablefrom] [query]

    where statusflag = 0'

    set @sql = replace(@sql, '[tableto]', @tableto)

    set @sql = replace(@sql, '[moveto]', @moveto)

    set @sql = replace(@sql, '[tablefrom]', @tablefrom)

    set @sql = replace(@sql, '[query]', @query)

    exec (@sql)

    print (@sql)

  • Create a linked server connection and use a

    SELECT ... INTO {linkedserver}.[{database}]..[{table}] FROM [{database}]..[{table}] WHERE statusflag = 0

    Update the statusflag on the source table after the insert (perhaps include an inner join with the destination table if during the transfer other records in the source will get a value 0 for statusflag).

    UPDATE [{database}]..[{table}]

    SET statusflag = 1 WHERE statusflag = 0

    FROM [{database}]..[{table}] source

    INNER JOIN {linkedserver}.[{database}]..[{table}] target

    ON source.ID = target.ID

    WHERE target.ID = 0

    Off course the above solution needs to be adjusted to your needs....

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • The reason that doesn't work in my case is that the staging tables have certain columns (like statusflag) that are used to determine good vs. bad data, and then only the good data is moved to the live tables. So, aside from those, all the other columns are the same. It basically pulls the trimmed list of columns I want to move from the staging table to the live table. I realize that a use case like that is pretty narrow, but I have to do it a few times a week.

  • erikd (6/26/2013)


    , but I wanted to try it without a cursor to build the column list.

    The code you posted in the first post does NOT contain a cursor. It does contain dynamic SQL, but you can't get passed that if you want to use this code for transferring data from different tables. The multiple SET REPLACE statements could be combined into one single statement, but that doesn't improve readability nor does it improve performance.

    So your approach is correct if you want to be flexible with your code. Keep in mind that DDL changes on the source needs to be replicated to the target. Your code will brake if the table definition on the source has columns that aren't present in the table definition on the target.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • HanShi (7/1/2013)


    erikd (6/26/2013)


    , but I wanted to try it without a cursor to build the column list.

    The code you posted in the first post does NOT contain a cursor. It does contain dynamic SQL, but you can't get passed that if you want to use this code for transferring data from different tables. The multiple SET REPLACE statements could be combined into one single statement, but that doesn't improve readability nor does it improve performance.

    So your approach is correct if you want to be flexible with your code. Keep in mind that DDL changes on the source needs to be replicated to the target. Your code will brake if the table definition on the source has columns that aren't present in the table definition on the target.

    I use the SET/REPLACE statements because when I try to hash out the correct number and placement of single quotes in things like this, I usually end up with a twitch.

  • Yeah, I know. It can be a pain to get code with single, double, triple, quadriple and sometimes even more quotes. Your solution with the REPLACE is a good appraoch.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **

Viewing 6 posts - 1 through 5 (of 5 total)

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