?
Solved

create autonumber id in views

Posted on 2006-10-30
7
Medium Priority
?
2,292 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 800 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Assisted Solution

by:CIC Admin
CIC Admin earned 800 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

764 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