Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends i want to copy data from one table to another table i know we can do this task using following query

INSERT INTO Destination
(EnrollNo,
CheckIn,
CheckOut,
DiffTime)
SELECT EnrollNo,
CheckIn,
CheckOut,
DiffTime
FROM Source

but here is problem ... i want to add a new colum with roll number which is started from 1 to end ... i will give a PK with autonumbreing ... after this 2nd colum with autonumbering without PK but it should be start from 1 to end of running query ..
Thanks in advance `
Posted
Comments
__TR__ 14-Dec-12 1:32am    
What's the use of having a second column with auto-numbering when you already have a PK column with auto-numbering ?

Friends auto increment will be imposed on PK from SQL server ... other column of number will be start from 1 to end of the query ...
means ... i transfer 20 record from one table to another table last PK will be 20
and last record of 2nd column = 20

after this query i have transfer another 10 record here pk will be 30
but 2nd column will start from 1 to 10
...
 
Share this answer
 
Comments
StackQ 14-Dec-12 1:40am    
u r right.
__TR__ 14-Dec-12 2:09am    
This is not an answer. Use "Have a question or comment?" button if you want to add any specific comments or questions and delete this solution to avoid getting downvoted.
To answer your question you can use ROW_NUMBER[^] to generate consecutive numbers.
Check this link[^]
In Identity Specification change identity seed value by the number you want
 
Share this answer
 
u can use this->

select * into UrNewTable_Name from SourceTable_Name;

to copy all data from SourceTable to NewTable

then u can add column as->

ALTER TABLE table_name
ADD column_name datatype;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900