tags:

views:

68

answers:

3

I have a table with one id (autonumber) field and 50 other fields. The table is normalized, these are 50 material properties etc.

I want to copy a record from this table into the same table - only the autoincrement id will be different. The query I am using now is

INSERT INTO tableName (field1,field2,....field50)
SELECT field1,field2,....field50 from tableName
WHERE autoid=1234;

Note that I have to type in ALL the 50 field names, twice! Is there any way to shorten this query so I don't have to type all of them?

Thanks.

+2  A: 

You could say:

INSERT INTO tableName
SELECT * from tableName WHERE autoid=1234;
Justin Ethier
@justin, Out of curiosity, wont this try to insert the AutoNumbered ID column as well as the others, resulting in a possible Primary Key Violation? although, in your defense, the query posted by the OP does seem to have some ambiguity, but i think that AutoID is the ID, not Field1
TerrorAustralis
Yes, autoId is the id field not one of the fields that need to be copied.
Soundar Rajan
I modified justins answer and posted it. It simply involved extracting the values, removing the ID and posting back in
TerrorAustralis
Good point, if this is part of the primary key then yes, you will have to use Dave's solution.
Justin Ethier
+1  A: 
INSERT INTO tableName
SELECT * from tableName 
WHERE autoid=1234;

If you have 50 columns in a table, maybe you should rethink about your database design?

ryanprayogo
i've never worked on an application that *didn't* have at least one or two tables with about 50 fields. :)
Kip
@Kip: I'm pretty sure that's not a Good Thing. ;)
msw
not good at all :)
Randy
@kip, my sympathies. I recently had to work on a database where there were on average ~200 columns per table, most of them null and the foreign keys had completely different names to their primary key counterparts in the lookup tables...
TerrorAustralis
+2  A: 

Looking at the above answers, the main issue is that you will get a primary key violation if you do insert into <tableName> select * from <tableName>

Here is my guess at a possible solution:

SELECT *
INTO #tempTable
FROM TableName
WHERE autoid = 1234

this gets your values. then you need to get rid of the ID

ALTER TABLE #tempTable
DROP COLUMN autoid

Then simply insert this back into the main table

INSERT INTO tableName
SELECT * FROM #tempTable

i'm not a gun at SqlServer, mainly having worked with sybase so there might be some syntactic issues. Please feel free to correct me, i will alter my answer

TerrorAustralis
I am using mysql. There seems to be no way to do an INSERT into #tmptable (is there?). Looks like I have to do "CREATE TEMPORARY TABLE" with all the fields and then insert into it.
Soundar Rajan
The standard SQL syntax is `SELECT [columns] INTO #Table` This should create a new table [called #Table] which has all the columns specified in the select and all the data specified in the WHERE clause, but it appears that MySQL doesnt support this (the blog gives some lame excuse about it not being needed). A workaround is to use this syntax: `CREATE TABLE #tempTable(SELECT * FROM TableName WHERE autoid = 1234)` This should in effect do the same thing
TerrorAustralis