URGENT!! Need help regarding primary key!!

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 22
Reputation: isaackhazi is an unknown quantity at this point 
Solved Threads: 0
isaackhazi isaackhazi is offline Offline
Newbie Poster

URGENT!! Need help regarding primary key!!

 
0
  #1
Nov 26th, 2008
I have converted around 395 Excel survey forms to MS SQl database with that many tables. Well, I run a script to assign a primary key on all the tables on my database.First I set the column property to NOT NULL and then i assign the primary key to it. I assign the primary key to a column called 'FPC_Code'. This column contains values such as, _C2348787, _C567489 etc etc......Well, my problem is that THE SECOND I ASSIGN THE PRIMARY KEY TO ALL THE TABLES THEY GET SORTED WRT THE FPC_Code column. I dont want this sorting to take place. I need it to remain unsorted as they are survey forms and would like to preserve the order. I am running out of time and i seriously need help. This is the script I run on all the tables on my database.

ALTERING COLUMN TO NOT NULL

  1.  
  2. DECLARE Alter_tables_cursor CURSOR
  3. FOR
  4. SELECT table_name FROM information_schema.TABLES WHERE table_name<>'dtProperties' AND table_type<>'VIEW'
  5. OPEN Alter_tables_cursor
  6. DECLARE @tablename sysname
  7. FETCH NEXT FROM Alter_tables_cursor INTO @tablename
  8. WHILE ( @@FETCH_STATUS = 0 )
  9. BEGIN
  10. PRINT 'Altering NOT NULL --> '+@tablename+' '
  11. EXEC('ALTER TABLE '+@tablename+' ALTER COLUMN FPC_CODE varchar(255) NOT NULL')
  12. FETCH NEXT FROM Alter_tables_cursor INTO @tablename
  13.  
  14. END
  15. PRINT 'All user-defined tables have been Altered.'
  16. DEALLOCATE Alter_tables_cursor

ASSIGNING PK

  1. DECLARE Alter_tables_cursor CURSOR
  2. FOR
  3. SELECT table_name FROM information_schema.TABLES WHERE table_name<>'dtProperties' AND table_type<>'VIEW'
  4. OPEN Alter_tables_cursor
  5. DECLARE @tablename sysname
  6. FETCH NEXT FROM Alter_tables_cursor INTO @tablename
  7. WHILE ( @@FETCH_STATUS = 0 )
  8. BEGIN
  9. PRINT 'Altering Primary Key -->'+@tablename+' '
  10. EXEC('ALTER TABLE '+@tablename+' ADD PRIMARY KEY (FPC_Code)')
  11. FETCH NEXT FROM Alter_tables_cursor INTO @tablename
  12.  
  13. END
  14. PRINT 'All user-defined tables have been Altered.'
  15. DEALLOCATE Alter_tables_cursor
Last edited by peter_budo; Nov 28th, 2008 at 1:40 pm. Reason: Correcting closing code tags from [\code] to [/code]
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: URGENT!! Need help regarding primary key!!

 
0
  #2
Nov 28th, 2008
SQL Server creates primary keys (by default) as clustered - which means it gets sorted.

Create a NONCLUSTERED primary key and you should be all set.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC