Solved

create autonumber id in views

Posted on 2006-10-30
7
2,276 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

867 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

20 Experts available now in Live!

Get 1:1 Help Now