943,763 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1546
  • MS SQL RSS
Nov 26th, 2008
0

URGENT!! Need help regarding primary key!!

Expand Post »
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

MS SQL Syntax (Toggle Plain Text)
  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

MS SQL Syntax (Toggle Plain Text)
  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]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isaackhazi is offline Offline
22 posts
since Sep 2008
Nov 28th, 2008
0

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

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.
Reputation Points: 13
Solved Threads: 9
Junior Poster in Training
omrsafetyo is offline Offline
58 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Return database that a trigger was executed in
Next Thread in MS SQL Forum Timeline: Please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC