SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Foreign Keys without Index
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ahmeds08
Aged Yak Warrior

India
645 Posts

Posted - 03/14/2013 :  02:11:29  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
Dear Experts,
Does anyone has a script which finds all the foreign keys on all the databases on server that dont have an index on them.

Thanks,
Javeed.

chadmat
The Chadinator

USA
1974 Posts

Posted - 03/14/2013 :  14:12:56  Show Profile  Visit chadmat's Homepage  Reply with Quote
This will work for the current database


select *
from sys.objects o join sys.indexes i on o.parent_object_id = i.object_id
where o.type = 'f'
AND i.type_desc = 'HEAP'
AND o.object_id NOT IN (SELECT o.object_id FROM sys.objects
join sys.indexes i on o.parent_object_id = i.object_id
where o.type = 'f'
AND i.type_desc = 'NONCLUSTERED')

-Chad
Go to Top of Page

ahmeds08
Aged Yak Warrior

India
645 Posts

Posted - 03/15/2013 :  04:50:59  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
quote:
Originally posted by chadmat

This will work for the current database


select *
from sys.objects o join sys.indexes i on o.parent_object_id = i.object_id
where o.type = 'f'
AND i.type_desc = 'HEAP'
AND o.object_id NOT IN (SELECT o.object_id FROM sys.objects
join sys.indexes i on o.parent_object_id = i.object_id
where o.type = 'f'
AND i.type_desc = 'NONCLUSTERED')

-Chad



Thanks for that.
I am looking for something which can check on all the databases on the server.

Thanks,
Javeed.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/15/2013 :  13:00:00  Show Profile  Reply with Quote
use sp_Msforeachdb for looping through dbs

try like


EXEC sp_Msforeachdb 'select * 
from sys.objects o join sys.indexes i on o.parent_object_id = i.object_id
where o.type = ''f'' 
AND i.type_desc = ''HEAP''
AND o.object_id NOT IN (SELECT o.object_id FROM sys.objects 
join sys.indexes i on o.parent_object_id = i.object_id
where o.type = ''f'' 
AND i.type_desc = ''NONCLUSTERED'')'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ahmeds08
Aged Yak Warrior

India
645 Posts

Posted - 03/18/2013 :  02:47:38  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
quote:
Originally posted by visakh16

use sp_Msforeachdb for looping through dbs

try like


EXEC sp_Msforeachdb 'select * 
from sys.objects o join sys.indexes i on o.parent_object_id = i.object_id
where o.type = ''f'' 
AND i.type_desc = ''HEAP''
AND o.object_id NOT IN (SELECT o.object_id FROM sys.objects 
join sys.indexes i on o.parent_object_id = i.object_id
where o.type = ''f'' 
AND i.type_desc = ''NONCLUSTERED'')'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Thanks Visakh.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/18/2013 :  12:53:54  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.17 seconds. Powered By: Snitz Forums 2000