You are here: Re: Primary key generating « MsSQL Server « IT news, forums, messages
Re: Primary key generating

Posted by Erland Sommarskog on 08/26/06 20:39

(mutemode@gmail.com) writes:
> I need to insert rows into a table from another table. The tables are
> identical column wise except the table im inserting from does not have
> a primary key value. On insert I need to generate a primary key that
> is consecutive based on the table im inserting into. Also the table im
> inserting into does not have a identity column. Much appreciated.

On SQL 2005:

INSERT target (ID, col2, col2, ....)
SELECT row_number OVER (ORDER BY <whatever you want to order by)>,
col1, col2, ...


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация