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

SQL 2005 - List Primary Key Columns Expand / Collapse
Author
Message
Posted Sunday, March 23, 2008 8:25 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 12:59 PM
Points: 287, Visits: 386
Comments posted to this topic are about the item SQL 2005 - List Primary Key Columns
Post #473303
Posted Monday, January 4, 2010 5:31 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:45 PM
Points: 17,947, Visits: 15,941
Nice idea and this is a nice foundation script.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #841801
Posted Friday, June 29, 2012 7:08 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 10:19 AM
Points: 305, Visits: 1,141
I like the ides of this, I would add ordering the columns as they are defined in the key. And for the programmers in my area I would add a statement in the where clause so they only got the table they need instead of every table on the database.

SELECT p.TABLE_NAME, c.CONSTRAINT_NAME, c.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS p
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE c on
c.TABLE_NAME = p.TABLE_NAME
AND c.CONSTRAINT_NAME = p.CONSTRAINT_NAME
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND p.TABLE_NAME = 'name_here'
ORDER by c.TABLE_NAME, c.ordinal_position
Post #1323062
Posted Friday, June 29, 2012 8:25 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 10:19 AM
Points: 305, Visits: 1,141
Here is a query to find all the indexes on a table.

select r.[name] as table_name, i.[name] as index_name, c.[name] as column_name
FROM sys.index_columns l
inner join sys.tables r on
l.object_id = r.object_id
inner join sys.indexes i on
l.object_id = i.object_id
and l.index_id = i.index_id
inner join sys.columns c on
l.object_id = c.object_id
and l.column_id = c.column_id
where r.[name] = 'table_name_here'
order by r.[name], l.index_id, l.key_ordinal
Post #1323099
Posted Friday, June 29, 2012 8:36 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 11:53 AM
Points: 150, Visits: 651
Nice idea. I tested it on 2000, 2005, and 2008. It works well on all of them.
Post #1323104
Posted Friday, June 29, 2012 10:03 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 9:02 PM
Points: 53, Visits: 145
I ran this and got an empty results set. Is there something that needs to be done to populate these schema views? This is a totally new subject for me. I was under the impression that these views would be already defined in the system and can just be queried as in this article.

Dana
Post #1323162
Posted Friday, June 29, 2012 10:11 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 10:19 AM
Points: 305, Visits: 1,141
It could be either your table does not have an index set up, or the database doesn't have any tables that have indexes.

You need to be connected to the database where the table exists for either of these to work.

I don't think anything needs to be done for this to work, but then I'm not a DBA.
Post #1323164
Posted Friday, June 29, 2012 12:28 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 9:02 PM
Points: 53, Visits: 145
I have plenty of tables, I've been using it for a while. And there are indexes. And stored procedures and functions. But still, the script returns an empty results set. It seems that something must be done to populate these views. I'm no DBA either, except by necessity. The database is on my shared hosting, so maybe there are some constraints there. I know there are other things that I do not have access to. For example, there have been scripts that access certain system stored procedures and are meant to help with optimizing and finding where things could be sped up. They don't work, either. But in those cases I get an error message that states that I don't have privileges to those procedures. This doesn't complain, it just returns and empty set. *shrug*

Dana
Post #1323224
Posted Friday, June 29, 2012 12:41 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 10:19 AM
Points: 305, Visits: 1,141
I talked to my DBA and he said these are automatically updated. So it must be a permissions issue that is stopping you from seeing anything.
Post #1323229
Posted Friday, June 29, 2012 1:01 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 9:02 PM
Points: 53, Visits: 145
belowery (6/29/2012)
I talked to my DBA and he said these are automatically updated. So it must be a permissions issue that is stopping you from seeing anything.


Thank you. At least I know. I suppose I can get along without them. I have thus far.

Dana
Post #1323240
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse