Solved

SQL 2008R2:  Looking for a fast way to do a 'INSERT INTO target <all columns except two> SELECT <all columns> FROM source'

Posted on 2014-12-04
8
196 Views
Last Modified: 2014-12-04
Hi All

I'm doing a SQL-to-SQL conversion, and have 50+ tables to convert from old (source) to new (target) database.   I think the answer is 'there's no really fast way to do this', but I'll ask the question anyways.

Each 'group' has..
Two Source tables:  Anywhere from 10 to 700 columns.  
   These two tables have the same schema, although some columns have different collations.
Target tables:  Number of columns = Columns in source tables + 2, as I added start_dt and end_dt.

I can't do a 'INSERT INTO Target SELECT * FROM Source' because of the two extra columns.

Question:  What's the fastest way to do a  'INSERT INTO target <all columns except two> SELECT <all columns> FROM source'?

Using a view in the designer I don't see a way to select all and have it show all columns, and then just remove the two I don't need.  * displays as * instead of all column names.

I'll entertain third party apps on this one.

Thanks.
Jim
0
Comment
Question by:Jim Horn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40480892
I can't do a 'INSERT INTO Target SELECT * FROM Source' because of the two extra columns.

you can! if you do this:

INSERT INTO Target SELECT s.*, null, null FROM Source s

or

INSERT INTO Target SELECT s.*, getdate(), getdate() FROM Source s
0
 
LVL 65

Author Comment

by:Jim Horn
ID: 40480896
typo, corrected in the original question, and the position of the start_dt and end_dt columns are the third and fourth column, not at the end.
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40480911
:)

insert into target (col1, col2, ... , coln) select (col1, col2, null, null, col3, ... , coln) from source

and getting column names should not be so difficult, right click, design, select all columns, copy & paste into your sql
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 65

Author Comment

by:Jim Horn
ID: 40480917
Correct, although my question asks if there's a more automated solution, as I'd rather avoid that as some of these tables have 700+ columns.
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40480924
or drop those two columns from target

do insert into target select * from source

then add those 2 columns at the end (or you can insert those columns in 3rd&4th position)
or add them to the end and then use design table to move those columns to the proper location (why do you need those ones in 3rd & 4th position, and why not the end?)
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40481087
If the table names match, something like below.  If not, we can join on relative column #, with offsets for the 2 extra columns.  May need some tweaking but you should be able to gen the code pretty quickly for all tables, then run it as needed:


DECLARE @source_table sysname
DECLARE @target_table sysname
DECLARE @col_list varchar(max)
DECLARE @sql varchar(max)

--naturally replaced by cursor table loop in final code
SET @source_table = 'dbs'
SET @target_table = 'dbs'

SELECT @col_list = STUFF((
    SELECT ', ' + src.name
    FROM sys.columns src
    INNER JOIN sys.columns trg ON
        trg.name = src.name
    WHERE
        src.object_id = OBJECT_ID(@source_table) AND
        trg.object_id = OBJECT_ID(@target_table)
    ORDER BY src.column_id
    FOR XML PATH('')
    ), 1, 1, '')

SET @sql = 'INSERT INTO ' + @target_table + ' ( ' + @col_list + ' ) SELECT ' + @col_list + ' FROM ' + @source_table + ' '
PRINT @sql
--EXEC (@sql)
0
 
LVL 65

Author Closing Comment

by:Jim Horn
ID: 40481453
Thanks Scott.  Ran like a champ..

Of the two source tables the second one was the same name as the first + a _History suffix, so I was able to do the below to populate in one run
SET @sql = 'INSERT INTO ' + @target_table + ' ( ' + @col_list + ' ) SELECT ' + @col_list + ' FROM ' + @source_table + ' '
EXEC (@sql) 

SET @sql = 'INSERT INTO ' + @target_table + ' ( ' + @col_list + ' ) SELECT ' + @col_list + ' FROM ' + @source_table + '_History '
EXEC (@sql) 

Open in new window


Then for each target table I ran the below to eliminate any duplicates
;WITH cte as (
 SELECT id, SystemModstamp, ROW_NUMBER() OVER (PARTITION BY id ORDER BY (SELECT 0)) as row_number
 FROM SF_Market) 
DELETE FROM cte WHERE row_number > 1

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40481456
CORRECTION to wording; instead of: "If the table names match, ...", it should be:
If the COLUMN names match, ...
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Format Date fields 11 65
Can we attach PDF to table 2 46
File attachment in the SQL Database from application 10 48
Restore a log backup compressed 3 18
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question