|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Moving data from one SQL table to another
Hi,
I am new to using SQL and I wonder if someone could give me a bit of assistance. I need to be able to move records from one SQL table to another, but I also need to read the previous entry in one of the columns and increment the number by one each time I move a record over. Can anyone help because I am completely lost. Regards John |
|
#2
|
|||
|
|||
|
You have to loop thru your result set. Either use SQL cursor or create a recordset with ADO, f.e.
|
|
#3
|
|||
|
|||
|
Is the number important or does it just have to be in sequence? If you just want a number in sequence, then create the column as an IDENTITY type field and don't specify the column in your insert statement. SQL Server will then automatically fill up the column for you.
Code:
CREATE TABLE tableCopy (
column1 IDENTITY(1,1),
column2 ....
columnxxx
)
INSERT INTO tableCopy(
column2,
column3,
... columnxxx
)
SELECT col2,
col3
col4
FROM tableOriginal
Notice that column1 is declared as IDENTITY, but is not specified in the INSERT statement. IDENTITY(1,1) means start the column value from 1 and increment by 1 for every row inserted. If you want to start from say 15, then you would declare it as IDENTITY(15, 1)
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. |
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > Moving data from one SQL table to another |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|