Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

sp to move data between matching tables Expand / Collapse
Author
Message
Posted Wednesday, June 26, 2013 1:00 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 2:20 PM
Points: 434, Visits: 1,763
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)

Post #1467819
Posted Thursday, June 27, 2013 5:18 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:44 PM
Points: 2,310, Visits: 2,790
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’! **
Post #1468090
Posted Saturday, June 29, 2013 8:05 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 2:20 PM
Points: 434, Visits: 1,763
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.

Post #1468798
Posted Monday, July 1, 2013 12:32 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:44 PM
Points: 2,310, Visits: 2,790
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’! **
Post #1468942
Posted Monday, July 1, 2013 8:52 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 2:20 PM
Points: 434, Visits: 1,763
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.

Post #1469098
Posted Monday, July 1, 2013 11:44 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:44 PM
Points: 2,310, Visits: 2,790
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’! **
Post #1469292
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse