Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a row number to new table in SQL?

I'm trying to create a new table using an existing table already using:

INSERT INTO NewTable (...,...)
   SELECT * from SampleTable

What I need to is add a record number at the beginning or the end, it really doesn't matter as long as it's there.

Sample Table

Elizabeth  RI 02914
Emily      MA 01834

Prospective New Table

1 Elizabeth  RI 02914
2 Emily      MA 01834

Is that at all possible?

This is what I ultimately I'm shooting for... except right now those tables aren't the same size because I need my ErrorTemporaryTable to have a column in which the first row has a number which increments by the previous one by one.

declare @counter int
declare @ClientMessage varchar(255)
declare @TestingMessage carchar(255)
select @counter = (select count(*) + 1 as counter from ErrorValidationTesting)
while @counter <= (select count(*) from ErrorValidationTable ET, ErrorValidationMessage EM where ET.Error = EM.Error_ID)
begin
    insert into ErrorValidationTesting (Validation_Error_ID, Program_ID, Displayed_ID, Client_Message, Testing_Message, Create_Date)
    select * from ErrorTemporaryTable

    select @counter = @counter + 1

end
like image 632
user1772421 Avatar asked Oct 24 '12 20:10

user1772421


People also ask

How do I add a number to a SQL query?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.

What is the ROW_NUMBER () in SQL?

ROW_NUMBER function is a SQL ranking function that assigns a sequential rank number to each new record in a partition. When the SQL Server ROW NUMBER function detects two identical values in the same partition, it assigns different rank numbers to both.

What command do you use to add rows to a table?

To add rows, click Insert Above or Insert Below and to add columns, click Insert Left or Insert Right. Tip: To add a row at the end of a table, click the last cell of the last row, and then press the TAB key.


3 Answers

You can use into clause with IDENTITY column:

SELECT IDENTITY(int, 1,1) AS ID_Num, col0, col1
INTO NewTable
FROM OldTable;

Here is more information

You can also create table with identity field:

create table NewTable
(
  id int IDENTITY,
  col0 varchar(30),
  col1 varchar(30)
)

and insert:

insert into NewTable (col0, col1)
SELECT col0, col1   
FROM OldTable;

or if you have NewTable and you want to add new column see this solution on SO.

like image 63
Robert Avatar answered Oct 22 '22 06:10

Robert


INSERT INTO NewTable (...,...)
SELECT ROW_NUMBER() OVER (ORDER BY order_column), * from SampleTable
like image 23
Aleksandr Fedorenko Avatar answered Oct 22 '22 04:10

Aleksandr Fedorenko


If you are in SQL Server

INSERT INTO newTable (idCol, c1,c2,...cn)
SELECT ROW_NUMBER() OVER(ORDER BY c1), c1,c2,...cn
FROM oldTable
like image 2
Kaf Avatar answered Oct 22 '22 06:10

Kaf