SQL command to ALTER!! PLease Help

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

SQL command to ALTER!! PLease Help

 
0
  #1
Sep 26th, 2008
I have a huge database with around 250 tables. But all the tables are the same ie. they all have all the same columns. These are survery forms from different stores and thats why they all have the same schema. When i converted all these files from excel sheets into one SQL Server 2008 database NO Primary Keys were assigned to any of the columns in any of the tables. Its not practical for me to go into each table and set the primary key. I need to know how i can set the primary keys for the same column in all the tables.

Database name: FAB
all my tables have the following columns:
osd | ood | Price | FPC_Code | FPC_Description | prdabbv


All my columns are of the type varchar(255).
Basically, All the tables in my database look exactly the same as the one above.
FPC_ code is a column that is never NULL and UNIQUE. So ideally i would like to set the FPC_Code column in all my tables as a primary key. Im not that great at SQL scripts so please help me.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: SQL command to ALTER!! PLease Help

 
0
  #2
Sep 26th, 2008
i would create a stored procedure with a cursor

use this to get the table names

  1. SELECT name
  2. FROM dbo.sysobjects
  3. WHERE xtype = 'U'

and this dynamic sql to set key
  1. exec('alter table ' + @tablename + ' ADD PRIMARY KEY(osd)');
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: SQL command to ALTER!! PLease Help

 
0
  #3
Sep 28th, 2008
I didn't test this but if I remember right the syntax goes:

  1. Imports System.Data.OleDb
  2.  
  3. Dim strSQL As String
  4. Dim oConn As OleDb.OleDbConnection
  5. Dim oCmd As OleDb.OleDbCommand
  6.  
  7. oConn = New OleDb.OleDbConnection("<connection string>")
  8. strSQL = "ALTER TABLE <table name> ALTER COLUMN FPC_Code CONSTRAINT FPC_Code_PK PRIMARY KEY"
  9. oCmd = New OleDb.OleDbCommand(strSQL, oConn)
  10. oCmd.ExecuteNonQuery()

and this is for the combined table and assuming FPC_Code is still UNIQUE in the combined table. If you want to add PK for each hundred or so tables, dickersonka's answers gives a good starting point.
Reply With Quote Quick reply to this message  
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

Re: SQL command to ALTER!! PLease Help

 
0
  #4
Sep 28th, 2008
thanx a lot guys.....ill try it out n let ya know....
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