Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to find out the identity coloumn of a table. Expand / Collapse
Author
Message
Posted Wednesday, May 10, 2006 4:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, May 10, 2007 1:18 AM
Points: 8, Visits: 1

Hi,

 

I want to drop the identity property of a coloumn in a table.But i dont know the column which has the identity property.

so please let me know How to find out the idntity column and how to drop the identity property of that coloumn.

ThanQ.

Post #278866
Posted Wednesday, May 10, 2006 5:12 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 3:35 PM
Points: 2,393, Visits: 3,399
SELECT identitycol FROM WhatEverTable WHERE 1 = 0


N 56°04'39.16"
E 12°55'05.25"
Post #278869
Posted Wednesday, May 10, 2006 5:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, May 10, 2007 1:18 AM
Points: 8, Visits: 1

ThanQ for the solution.

But i want to drop the identity property of that coloumn.

How can i do it in TSQL. please let me know.

 

Post #278883
Posted Thursday, May 11, 2006 5:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Today @ 7:45 AM
Points: 7, Visits: 38

Hi,

hope it will help you

------------------------------------------

declare @TblName sysname

set @tblName = 'Your table name'

-- show identity col name
select syscolumns.name from sysobjects
inner join syscolumns on sysobjects.id = syscolumns.id
where sysobjects.name = @tblName and syscolumns.status = 0x80

-- show identity constraint name
select sysobjconstraint.name from sysobjects
inner join sysconstraints on sysobjects.id = sysconstraints.id
left outer join sysobjects sysobjconstraint on sysconstraints.constid = sysobjconstraint.id
where sysobjects.name = @tblName and sysobjconstraint.xtype = 'PK'

---------------------------------------------

use the resultset into ALTER TABLE DROP CONSTRAINTS but be carefull with FK, for detecting FK take a look at sysforeignkeys table

Post #279176
Posted Thursday, May 11, 2006 6:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 12,890, Visits: 31,853

there's a difference between a primary key, which technically can span multiple columns, and a column that has the identity function on it to auto generate the next value of the table;

there can be only one column in a table where the autoval is not null(which identifies it as the column that was created with identity)

 

with both the drop constraint for the PK and the alter table to remove the identity(), i think that's the tools you need.

here i'm expanding Gracia's example to include that as well:

declare @TblName sysname

set @tblName = 'GMACT'

-- show identity col name
select
 'ALTER TABLE '
  + @tblName
  + ' ALTER COLUMN '
  + UPPER(syscolumns.name)
  + SPACE(2)
  + UPPER(TYPE_NAME(syscolumns.xtype))
  --this is for the original definition
  --+ SPACE(12 - LEN(TYPE_NAME(syscolumns.xtype))) + CASE WHEN syscolumns.autoval IS NULL THEN '              ' ELSE ' IDENTITY(1,1)' END
  + SPACE(2) + CASE WHEN syscolumns.isnullable=0 THEN  ' NOT NULL' ELSE '     NULL' END AS ORIGINAL_DEFINITION,
syscolumns.name from sysobjects
inner join syscolumns on sysobjects.id = syscolumns.id
where sysobjects.name = @tblName and autoval is not null

-- show identity constraint name
select 'ALTER TABLE ' + @tblName + ' DROP CONSTRAINT ' + sysobjconstraint.name  AS DROP_STATEMENT,sysobjconstraint.name from sysobjects
inner join sysconstraints on sysobjects.id = sysconstraints.id
left outer join sysobjects sysobjconstraint on sysconstraints.constid = sysobjconstraint.id
where sysobjects.name = @tblName and sysobjconstraint.xtype = 'PK'

--select * from syscolumns where autoval is not null

 

results:

ORIGINAL_DEFINITION                                      
----------------------------------------------------------
ALTER TABLE GMACT ALTER COLUMN ACTTBLKEY  INT   NOT NULL 
                                                         
DROP_STATEMENT                                           
----------------------------------------------------------
ALTER TABLE GMACT DROP CONSTRAINT PK__GMACT__1E05700A    



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #279201
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse