Solved

create autonumber id in views

Posted on 2006-10-30
7
2,274 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
  • 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now