473,379 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

insert into xyz select * from abc

Have two tables "abc" and "xyz", where "xyz" is a superset, column-wise, of
"abc".

Is there any simple way to inject all the rows of "abc" into "xyz"?

Tried "insert into xyz select * from abc" but got a complaint of "column
count doesn't match value count at row 1", which of course is true.

Any way to tell SQL to simply set defaults for the missing columns?

Thanks.

Jul 20 '05 #1
3 5041
Frank Natoli wrote:
Have two tables "abc" and "xyz", where "xyz" is a superset, column-wise,
of "abc".

Is there any simple way to inject all the rows of "abc" into "xyz"?

Tried "insert into xyz select * from abc" but got a complaint of "column
count doesn't match value count at row 1", which of course is true.

Any way to tell SQL to simply set defaults for the missing columns?


Assuming the columns in abc and xyz are in the same order then you can
simply use select * and add in default values for all the fields that are
not defined in the abc table like so (the table name/alias is required for
the * if you have any vaules before it):

insert into xyz select null, abc.*, null, null, null from abc

Obviously you would need to change my nulls to whatever the appropriate
default values is (which cannot be null if the field is defined "not
null").

If the columns are in a different order then you need to write them all into
the query eg:

insert into xyz select column1, column2, column3, null, null from abc
--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 20 '05 #2
Frank Natoli wrote:
Tried "insert into xyz select * from abc" but got a complaint of "column
count doesn't match value count at row 1", which of course is true.

Any way to tell SQL to simply set defaults for the missing columns?


You could name all the input columns in xyz, and then any you don't
specify will use the default value defined in the DEFAULT clause of each
column when you created table xyz.

INSERT INTO xyz (xcol1, xcol2, xcol3)
SELECT acol1, acol2, acol3 FROM abc;

If you need to specify values other than those DEFAULTs defined for xyz
columns, use Chris Hope's suggestion.

Regards,
Bill K.
Jul 20 '05 #3
Bill Karwin wrote:
Frank Natoli wrote:
Tried "insert into xyz select * from abc" but got a complaint of "column
count doesn't match value count at row 1", which of course is true.

Any way to tell SQL to simply set defaults for the missing columns?


You could name all the input columns in xyz, and then any you don't
specify will use the default value defined in the DEFAULT clause of each
column when you created table xyz.

INSERT INTO xyz (xcol1, xcol2, xcol3)
SELECT acol1, acol2, acol3 FROM abc;

If you need to specify values other than those DEFAULTs defined for xyz
columns, use Chris Hope's suggestion.


Heh, another half asleep post by me before... I should have mentioned this
method as well. You only need to do the way I suggested if you need to
specify something other than the default value. The great thing about doing
it Bill's way is if you have an auto-incrementing primary key then you can
leave it out of the columns selected which may cause issues inserting into
the new table.
--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
0
by: jtocci | last post by:
I'm having a big problem with CREATE RULE...ON INSERT...INSERT INTO...SELECT...FROM...WHERE when I want to INSERT several (20~50) records based on a single INSERT to a view. Either I get a 'too...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
0
by: ImraneA | last post by:
Hi there I had pleasure of upsizing Access v97 db to Access v2K/SQL 2K. Wish to provide some knowledge gained back to community - hopefully help others. 1.Question how do you test stored...
4
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why...
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
0
by: magnolia | last post by:
i created a trigger that will record the changes made to a table .everything works fine except the insert query.whenerever i try to insert a record it fires insert and update triger at the same time...
0
by: gpspocket | last post by:
help me -CURSOR backward insert from End Date > to Start Date how to insert dates from end to start like this SELECT 111111,1,CONVERT(DATETIME, '17/03/2008', 103), CONVERT(DATETIME,...
6
by: lenygold via DBMonster.com | last post by:
Hi everybody: What is the best way to I have 10 tables with similar INSERT requiremnts. INSERT INTO ACSB.VAATAFAE WITH AA(AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP) AS ( SELECT AA_TIN,...
1
by: EJO | last post by:
with sql 2000 enterprise Trying to build a stored procedure that will take the rows of a parent table, insert them into another table as well as the rows from a child table to insert into...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.