Solved

create autonumber id in views

Posted on 2006-10-30
7
2,287 Views
Last Modified: 2008-02-01
Maybe an easy one?

How can I create an autonumber field in a view.
I am joining tables in a view, but there is no unique id,
so I would like to create one

Anyone an idea?

thanks

Geert
0
Comment
Question by:Geert Bormans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Accepted Solution

by:
CIC Admin earned 200 total points
ID: 17838405
0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17838406
insert into a table with an identity column... only way I can think of.  Oracle has rownum but SQL doesn't.

select top 0 * into newtable from myview - just creates structure to match view
alter table newtable add seq int identity(1,1)
insert newtable(allcolumns EXCEPT the new sequence) select columns from myview

0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17838417
Kent's link is better
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 5

Assisted Solution

by:CIC Admin
CIC Admin earned 200 total points
ID: 17838460
0
 
LVL 60

Author Comment

by:Geert Bormans
ID: 17840863
Thanks,

The views are pretty complex,
so I avoided things like copying parts of the view to get the count(*)
or setting an IDENTITY... INTO

I decided to go for
select
    NewID() as id,
...

It doesn't give me autonumbering
but it gives me a unique ID, though an ugly one :-)
I gave that priority over a more complex approach

cheers

Geert
0
 
LVL 5

Expert Comment

by:CIC Admin
ID: 17844602
Another thought : If the ids did not need to be sequential and you just wanted a unique id, AND if both tables had their own autonumber id AND you knew there would not be mroe than x records in a particular table (a lot of ANDs in there I know), what you could do is generate a unique id by combining the two ids.  For instance, if you know that table two was not going to have over 10000 records, then you could take (table1ID * 10000) + table2ID.  This would give you a number that is unique regardless of which combination of table1 records and table2 records are joined.  This could of course be extrapolated to 3 or more tables.  They would not be sequential but it may serve your needs in some circumstances.

P.S.  Thanks for the points, but was the assist meant for AaronAbend?  I got points for both the Accept and the Assist.
0
 
LVL 60

Author Comment

by:Geert Bormans
ID: 17850817
Kent,

thank you for your last suggestion
I had thought about that but the mult-to-multi link table has no id's (legacy database... I know :-)
so I possibly would end up with duplicate id's this way

With my points I wanted to indicate to future readers that there was valuable information in two messages from yourself,
so no mistake in awarding.
I could not implement AaronAbend's solution (will all respect and a thankyou for his/her input though)

cheers

Geert
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question