[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

create autonumber id in views

Posted on 2006-10-30
7
Medium Priority
?
2,298 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Technology Partners: 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!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
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 extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

834 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