Posted on 2012-03-13
Last Modified: 2012-03-13
We have a table with hundreds columns, so it would be a bit heavy to name each, and we need a stored procedure that copies one row. We have a primary key autonum, so :
insert into myTable select * from myTable does not work.
Any (simple) idea ?
Thanks for help
Question by:javilmer
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

Expert Comment

ID: 37714454
Are you wanting any record or a specific record?

Something like:

insert into myTable
select  Top 1 * from myTable
LVL 15

Accepted Solution

Anuj earned 500 total points
ID: 37714462
You cannot use '*' in your insert queries if you have identity columns in the destination table,
a tricky way to get all non identity columns is by using the result of the following query.

FROM sys.columns
WHERE is_identity = 0
AND object_id = OBJECT_ID('YourTable')
FOR XML PATH('')),1,1,'')

Author Comment

ID: 37714650
anujnb : that's sounds interesting, how can i do the insert though ?
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

LVL 15

Expert Comment

ID: 37714712
Execute the above query, this will gives a list of columns separated by comma, copy the results and paste into your Stored procedure, this is just a tricky way of doing the things you can do the same using dynamic sql but its not recommended.

Author Comment

ID: 37714867
I can't do it manually, if not i would copy/paste in visual studio, I need a stored proc because the application needs a "copy function" :)
LVL 25

Expert Comment

ID: 37714959
With a identity-column you mustalways specify the columnlist

insert into mytable (col1,col2 ...;)
select col1, col2
from ....

If you also want to insert the identity-column itself you must SET IDENTITIY_INSERT

And I would advise always include the column-list because for future changes in source or destination your sql could become invallid.

Notice that when in SSMS you select your table and click right, you can choose for that table to generate a insert statement for you .... no typing for the columnlist only copy/paste

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What type of testing am I doing? 4 76
how to resize column length with primary ket 4 34
Moving away from Access 2003 adp files 4 48
What does "Between" mean? 6 47
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 needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

734 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