Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8627

Re: Altering primary key in database

$
0
0

You'll need to do it like this:

 

- Drop Primary Key

- Alter column type

- Add Primary Key

 

These can all be done with execute immediate, and SA11 will let you check the col type from the sys.syscolumns. You'll need to know the original primary key. This can only be done if no-one else is using the database. Startup code would be an ideal place for it instead of an on demand button somewhere.

 

Example:

string ls_coltype, ls_exec

select sys.syscolumns.coltype into :ls_coltype from sys.syscolumns where cname='PKeyColName';

if ls_coltype="smallint" then

     ls_exec = 'alter table "TableNameHere" drop primary key'

     execute immediate :ls_exec;

 

     ls_exec = 'alter table "TableNameHere" modify '"KeyColName" integer'

     execute immediate :ls_exec;   

 

     ls_exec = 'alter table "TableNameHere" add primary key ("PKeyColName", .....)'

     execute immediate :ls_exec;

end if

 

Add error checking like you normally would.


Viewing all articles
Browse latest Browse all 8627

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>