Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.net SQL Create table from table with Identity column

Posted on 2014-10-22
6
Medium Priority
?
288 Views
Last Modified: 2014-10-22
Hi

I have a number of tables in a SQL database that don't have Identity columns
I know that you can copy a table using the following statement
SELECT * INTO Table2 FROM Table1
What SQL code would I use to copy a table but add an extra identity column
at the beginning of the table?
0
Comment
Question by:Murray Brown
  • 3
  • 3
6 Comments
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 40396529
You can use ROWNUMBER to produce a new column with codes:
SELECT ROW_NUMBER() OVER(ORDER BY Table1.ColumnName) AS NewID, *
INTO Table2
FROM Table1

Open in new window

0
 

Author Comment

by:Murray Brown
ID: 40396560
Hi
Thanks
I don't quite understand Table1.ColumnName
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40396586
I don't know your table structure so you need to substitute "Table1.ColumnName" with a real column name from your table.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:Murray Brown
ID: 40396608
If my column names are Col1,Col2,Col3 all of type nvarchar(40) can I select any of these columns?
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40396614
Using the '*' all your columns will be returned. The need for a column name it's only to order the rows so SQL Server can give them a number (an identity column for you). So you can keep the '*' and provide a column name to the ORDER BY clause (preferential a primary key column).
0
 

Author Closing Comment

by:Murray Brown
ID: 40396620
Thanks very much Vitor. I worked it out
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

885 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