views:

372

answers:

4

I've got 10 tables that I'm joining together to create a view. I'm only selecting the ID from each table, but in the view, each ID can show more than once, however the combination of all ID's will always be unique. Is there a way to create another column in this view that will be a unique ID?

I'd like to be able to store the unique ID and use it to query against the view in order to get all the other ID's.

+1  A: 

No you cannot do that in a view, what you can do is create a temporary table to hold all this information and create a key or unique id there for each row.

JonH
+1'ed. But, if he wants that information to persist, it wouldn't be a temporary table, it would be an actual table.
Mark Canlas
@Mark yep you are right :).
JonH
A: 

I think you can do that using ROW_NUMBER(), at least if you can guarantee an ordering of the view. For example:

 SELECT
     ROW_NUMBER() OVER (ORDER BY col1, col2, col3) as UniqueId
 FROM <lotsa joins>

As long as the order stays the same, and only fields are added at the end, the id will be unique.

Andomar
This will not guarantee that the numbers are the same every time. If I have (1, 1, 1) and (3, 3, 3) they will be 1 and 2 respectively. If I then add (2, 2, 2) then the (3, 3, 3) row will now become 3 and not 2.
Tom H.
@Tom H: That's why I put "if you can guarantee an ordering" at the top of my answer :)
Andomar
Guaranteeing an ordering doesn't do it though. It's not just the order of the rows it's also that no new rows can be added and rows can't be changed. In other words, the data would have to be completely static with the exception of adding rows to the very end in the ordering.
Tom H.
Yes, I unfortunately don't think that I can guarantee an order.What do you think about creating a table to contain all the records (basically just 10-12 IDs)??? Then my only concern would be updating that table each quarter when the database is updated.Any thoughts on that solution?
Brendo
@Tom H: That's literally what my answer says? @Brendo: A table works fine, as long as you have a way to uniquely identify a row in the view!
Andomar
A: 

The fact that you're attempting to do this points to much bigger problems in the database design and/or application architecture.

Since you have 10 tables and I'm going to guess that the DB designer(s) just slapped on ID INT IDENTITY to all of the tables you will end up having about (2^31)^10 possible rows in the table.

The only data type that I can think of that might cover that number would be to translate all of the integers into 0-padded strings and put them all together as a big CHAR.

My guess though is that your real problem isn't getting this ID for a view but some other thing that you're attempting to do, which is the question that you should be asking. Just a hunch though.

Tom H.
Yes, I completely agree, unfortunately its something that I'm stuck working with, just looking for the best way to go about it. It's a vehicle database, so there are 50+ tables each containing their own ID's that can all be joined to the next. I'm just trying to put together the basic information/IDs in order to run other queries from: VehicleID, BedID, BodyStyleID, BrakeID, DriveTypeID, EngineConfigID, etc... really not pretty.
Brendo
A: 

One possibility would be to call a function from your view that calculates a hash code on all of the ID columns. If you use a decent cryptographic hash algorithm, the odds of a collision are minuscule (probably more likely that the disk would deliver bad data). Even easier, you could of course just concatenate the different IDs into a single, much larger ID, perhaps as a binary or varbinary column.

Storing that ID and being able to query against it would be a bit more work. I can't think of a way to both compute it and store it from a view. You would probably need a stored procedure to create it first; the details depend heavily on the specifics of your app.

RickNZ