SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sp to move data between matching tables


sp to move data between matching tables

Author
Message
sqldriver
sqldriver
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2078 Visits: 2536
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)


HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8242 Visits: 3718
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’! **
sqldriver
sqldriver
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2078 Visits: 2536
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.
HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8242 Visits: 3718
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’! **
sqldriver
sqldriver
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2078 Visits: 2536
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.
HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8242 Visits: 3718
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’! **
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search