Wednesday, March 7, 2012

Drop an unnamed primary key....

Hello!
How can I drop an unnamed primary key with a query?
Else how can I recover its name assigned by SQLServer?
Thanks for your help> Else how can I recover its name assigned by SQLServer?
http://www.aspfaq.com/2104|||This is exact but if I'm connected to database as an user 'alpha' (which
is db_owner),
the clause 'c_obj.uid = user_id()' in query view prevents me from
recovering the query data
And I can't modify the query view on database.....
"Aaron Bertrand [SQL Server MVP]" a crit :

> http://www.aspfaq.com/2104|||Frdric Armani wrote:
> Hello!
> How can I drop an unnamed primary key with a query?
> Else how can I recover its name assigned by SQLServer?
> Thanks for your help
Just run sp_help on the table and look in the index and/or contraint
sections for the auto-generated name
create table dbo.Whatever123 (
col1 int not null primary key,
col2 int )
go
sp_help Whatever123
go
constraint_type constraint_name
-- --
PRIMARY KEY (clustered) PK__Whatever123__591CDF8E
drop table dbo.Whatever123
go
David Gugick
Quest Software
www.imceda.com
www.quest.com|||> the clause 'c_obj.uid = user_id()' in query view
What clause in what query view? Did you run:
SELECT
T.TABLE_NAME,
T.CONSTRAINT_NAME,
K.COLUMN_NAME,
K.ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE K
ON T.CONSTRAINT_NAME = K.CONSTRAINT_NAME
WHERE
T.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND T.TABLE_NAME = 'table_name'|||Hi Frederic,
To find the PK name, use this:
SELECT name FROM sysobjects
WHERE parent_obj=OBJECT_ID('YourTable') AND xtype='PK'
To drop the PK, use this (if you don't mind using an undocumented SP):
sp_execresultset 'SELECT ''ALTER TABLE ''
+QUOTENAME(OBJECT_NAME(parent_obj))
+'' DROP CONSTRAINT ''+QUOTENAME(name)
FROM sysobjects WHERE parent_obj=OBJECT_ID(''YourTable'')
AND xtype=''PK'''
Of course, replace YourTable with your table name.
Razvan

No comments:

Post a Comment