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 |
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-09-11 : 12:27:57
|
Good afternoonIs there a way to insert an ID column into a (view)which will auto-number each line based on a starting number? It would be awesome if there is a way to do this in a view.If so please let me know howThanks |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-09-11 : 12:43:43
|
If you were using SQL Server 2005, this would be very easy to add to it. But you can't add an identity value to a view.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-09-11 : 13:22:11
|
Hi tkizerThanks for the responce, understood.. I cannot add an identity value to a view.Question, then is it possible to add a column to a view and auto-number each line based on a starting number? for instance, say I started the line numbering at 50, thenthe next line would be 51, 52 and so on. Would this be possible? as I only need to number each line in the view. It dosen't have to be an IdentityIf this is possible, please let me knowThanksAgain |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-09-11 : 13:37:09
|
The easiest way to do it would be to put the view into a temp table and add an identity column to that table.SELECT IDENTITY(int, 1, 1) AS ViewID, ColumnA, ColumnB, ColumnC, ...INTO #TempFROM YourViewSELECT * FROM #TempWHERE ...But the better solution is just to number the rows using application code, since this is most likely a presentation issue.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-09-11 : 13:55:41
|
Hi tkizerOnce again thanks for the responce.. understood. given this i'll just go the suggested Temp table route.Question pertaining to the Temp table with an identity column. Would something like this work for auto-numberingINSERT INTO Temp (MyID Int IDENTITY(-1000000, 100) NOT NULL ,MyDescription NVarChar(50) NOT NULL)select * from TempGOSomeone here told me this would work, but I find getting solutions from yourselfand others on this site has worked the best for me, so I don't entirely trustthe solution noted above and thought i'd run it by the experts (you guys & gals)E |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-09-11 : 14:13:04
|
Yes you can do it that way too, although with slight syntax changes. You'll need to get familiar with SET IDENTITY_INSERT option though.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
|
|
|
|