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 12»»

Search Columns in all Databases Expand / Collapse
Author
Message
Posted Monday, February 6, 2012 9:07 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 9:14 AM
Points: 4,226, Visits: 4,278
I'm using the SQL Statement listed below to search the current database for column names.

What I need is a Statement to search all Databases.

Can I use Exec sp_MSforeachdb and if so what is the syntax to include the Database Name?

SELECT obj.Name AS TableName,col.Name AS ColumnName, col.xtype, col.length 
FROM Sysobjects AS obj
INNER JOIN syscolumns AS col ON obj.id = col.id
WHERE Obj.xtype = 'U'
AND col.name LIKE '%Salvage%'



For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Post #1247466
Posted Monday, February 6, 2012 9:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 5:36 PM
Points: 13,418, Visits: 12,283
Not exactly sure what you are asking. Are you trying to find all columns in all databases in a single result set?

Something like this should work for you.

create table #Cols
(
TableName nvarchar(128),
Columnname varchar(128),
xtype tinyint,
length int
)



EXECUTE master.sys.sp_MSforeachdb 'USE [?]; insert #Cols SELECT obj.Name AS TableName,col.Name AS ColumnName, col.xtype, col.length
FROM Sysobjects AS obj
INNER JOIN syscolumns AS col ON obj.id = col.id
WHERE Obj.xtype = ''U''
AND col.name LIKE ''%Salvage%'''

select * from #Cols

drop table #Cols



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1247486
Posted Monday, February 6, 2012 11:50 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 9:14 AM
Points: 4,226, Visits: 4,278
Thanks Sean.


What function do I user on the parent_object to get the Database Name?



For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Post #1247601
Posted Monday, February 6, 2012 11:55 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 5:36 PM
Points: 13,418, Visits: 12,283
This what you mean?

create table #Cols
(
DataBaseName varchar(128),
TableName nvarchar(128),
Columnname varchar(128),
xtype tinyint,
length int
)

EXECUTE master.sys.sp_MSforeachdb 'USE [?]; insert #Cols SELECT DB_NAME(), obj.Name AS TableName,col.Name AS ColumnName, col.xtype, col.length
FROM Sysobjects AS obj
INNER JOIN syscolumns AS col ON obj.id = col.id
WHERE Obj.xtype = ''U''
AND col.name LIKE ''%Salvage%'''

select * from #Cols

drop table #Cols



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1247603
Posted Monday, February 6, 2012 12:10 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 9:14 AM
Points: 4,226, Visits: 4,278
Yes, that is what I wanted.

Thanks.


For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Post #1247607
Posted Saturday, July 6, 2013 10:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, July 20, 2013 12:37 PM
Points: 2, Visits: 31
Hi Geeks,

I have question about usage of this SP that how legal it is use to use this sp?
what kind of impact it does on your sever when a statement with this sp is fired? I mean does it have any negative impacts on using this SP, since it comes under undocumented SPs.

Thank you!
Post #1470926
Posted Monday, July 8, 2013 6:48 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 4:32 AM
Points: 2,651, Visits: 4,731
shy.n86 (7/6/2013)
Hi Geeks,

I have question about usage of this SP that how legal it is use to use this sp?
what kind of impact it does on your sever when a statement with this sp is fired? I mean does it have any negative impacts on using this SP, since it comes under undocumented SPs.

Thank you!


I don't think that using this SP is "illegal"
It would not be advisable to use such SP's in Production code, but for ad-hoc requests I don't see any harm using this procedure
You can check the code of the procedure under master->Programmability->Stored Procedures->System Stored Procedures->sp_MSforeachdb



Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1471156
Posted Monday, July 8, 2013 10:42 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
This link might be useful:

sql-server-integrity-check


Post #1471433
Posted Tuesday, July 9, 2013 8:18 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Yesterday @ 11:46 AM
Points: 431, Visits: 1,738
I just used this yesterday, if you're ever in the market for a quick select:

sp_MSForEachDB @command1='USE ?;
SELECT
Table_Catalog
,Table_Schema
,Table_Name
,Column_Name
,Data_Type
,Character_Maximum_Length
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = ''NVARCHAR'' and COLUMN_NAME like ''Recall%'' and TABLE_NAME LIKE ''%SAMPLE%'''

Post #1471646
Posted Tuesday, July 9, 2013 9:04 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
shy.n86 (7/6/2013)
Hi Geeks,

I have question about usage of this SP that how legal it is use to use this sp?
what kind of impact it does on your sever when a statement with this sp is fired? I mean does it have any negative impacts on using this SP, since it comes under undocumented SPs.

Thank you!
There are no legal implications for you to use it, it is an undocumented stored procedure, which basically means we built it for our use and we don't want to support it for YOURS. So if you use it and it stops working the way you want or it previously had then TOUGH. The only thing even remotely legal about it comes down to you can't complain and have no legal position if you use it and it stops working for you.

As far as performance it is pretty marginal, considering what it is doing.. The statement YOU pass in to execute is much more likely to have a performance impact than it itself.

CEWII
Post #1471678
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse