Dynamic Primary key for view created by 'Union All' from same structure tables (500+)

Dear Friends,

i am working on projects which involve massive data of around many millions. what i want is there are 500  tables with same structure i have joined them with 'union all' in sql 2000. but i want to make a dynamic primary key (integer) that must be unique for every records merged in current view from 500 tables with same structure?


table Structure is
UnitName
Address
City
State
Zip
LVL 2
tomerpsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

awaiskhanCommented:
Hi,
Please see my code below,

I asume that you hve write the view already to retrive the records from the nth tables of similar structure. So I have write the code below. Hope it will help in your indeed.

(On the view you can't assign the primary key but you can make the table on that view with the primary key, by which you can take the unique column)


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Table4]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Table4]
GO

CREATE TABLE [dbo].[Table4] (
      [ID] [int] IDENTITY (1, 1) NOT NULL ,
      [UnitName] [varchar] (50)  NULL ,
      [Address] [varchar] (50)  NULL ,
      [City] [varchar] (50)  NULL ,
      [State] [varchar] (50)  NULL ,
      [Zip] [varchar] (50)  NULL
) ON [PRIMARY]
GO



INSERT INTO table4
SELECT * FROM VIEW1


--Thanks


tomerpsAuthor Commented:
Dear Khan,

Thanks for ur contribution but ur solution is not acceptable becuase i want that view to be dynamic all the time. there are many time possiblity of being database updated dailly so i have to run "create table x as select * from view1;" all the time.
so please look something new solution.

Thanks
Prithvi S Tomer
awaiskhanCommented:
Thanks for your comments!

Please see my next solution, in which I am assuming that UnitName field is primary key, and when you get the records from your view you get the unique values in the UnitName column.
If this than my next solution will give you the unique column.

SELECT UnitName,Address,City,State,Zip,
(SELECT COUNT(*) FROM VIEW1 e2 WHERE e2.UnitName <=e.UnitName) AS rownumber
FROM VIEW1 e
ORDER BY UnitName

--here VIEW1 I assume your's All Joins containing Veiw on the your tables.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.