Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
HowlingOdin
Starting Member
4 Posts |
Posted - 2011-10-26 : 14:02:03
|
| Hello,Running into a fairly basic problemFor the sake of simplicity, let's take this existing tableDate State Temp1-22 PA 861-23 TX 941-24 VA 88. . .. . .. . .What I'd like to do is to add an additional column called "P_ID" to use as the primary key. I want that key to incrementally increase by 1 for each row. If I create a new column, it will contain null values. I'd like to replace it with integers 1, 2, 3, 4, 5, etc. What is the best way in going about to do that? Everything I try with ROWNUMBER() or RANK() seems to set all of the P_ID values to 1, which isn't what I'm looking forExpected resultDate State Temp P_ID1-22 PA 86 1 1-23 TX 94 2 1-24 VA 88 3. . . 4. . .. . .any help is appreciated, thanks! |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-10-26 : 14:23:18
|
..but if you insistCREATE TABLE myTemps99 ([Date] datetime, [State] char(2), [Temperature] decimal(5,2))GOINSERT INTO myTemps99 ([Date], [State], [Temperature])SELECT '1-22-11', 'PA', 86 UNION ALLSELECT '1-23-11', 'TX', 94 UNION ALLSELECT '1-24-11', 'VA', 88GOSELECT * FROM myTemps99GOALTER TABLE myTemps99 ADD PK_Col int IDENTITY(1,1)GOSELECT * FROM myTemps99GODROP TABLE myTemps99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
HowlingOdin
Starting Member
4 Posts |
Posted - 2011-10-26 : 14:26:36
|
quote: Originally posted by X002548 wouldn't State and Date be the natural key of the table?Why do you want some erroneous number?
Hello Brett,This is just an example. This isn't the actual table. Just looking to find a way insert a new column in an already existing table which autoincrements by 1. My actual table is much larger and has a lot of uncessary information, which why I didn't post it here |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
HowlingOdin
Starting Member
4 Posts |
Posted - 2011-10-26 : 14:37:57
|
quote: Originally posted by X002548 ..but if you insistCREATE TABLE myTemps99 ([Date] datetime, [State] char(2), [Temperature] decimal(5,2))GOINSERT INTO myTemps99 ([Date], [State], [Temperature])SELECT '1-22-11', 'PA', 86 UNION ALLSELECT '1-23-11', 'TX', 94 UNION ALLSELECT '1-24-11', 'VA', 88GOSELECT * FROM myTemps99GOALTER TABLE myTemps99 ADD PK_Col int IDENTITY(1,1)GOSELECT * FROM myTemps99GODROP TABLE myTemps99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
Thanks! Didn't realize it was so simple. I was doing all sorts of funky stuff which didn't work out |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|
|