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 Programming
 Searching for a field type
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rmg1
Posting Yak Master

245 Posts

Posted - 07/04/2012 :  06:27:32  Show Profile  Reply with Quote
Hi all

I need to check all the fields in all tables in all databases to check if any are nvarchar.

Is there any "reasonably" quick way of doing this without checking each table in each database by hand?

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/04/2012 :  09:32:32  Show Profile  Reply with Quote
You can use the sp_MSforeachdb stored proc. It is an undocumented stored proc, but everyone seems to use it without a second thought. You would do something like this, for example:
exec sp_MSforeachdb 
'select 
	''?'' AS [Database],
	[COLUMN_NAME],
	[CHARACTER_MAXIMUM_LENGTH]
 from 
	[?].[Information_schema].[columns]
 where
	[DATA_TYPE] = ''nvarchar''';
Go to Top of Page

rmg1
Posting Yak Master

245 Posts

Posted - 07/04/2012 :  10:12:49  Show Profile  Reply with Quote
Thanks for that.
Is it possible to add in the table name as well?
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/04/2012 :  10:21:39  Show Profile  Reply with Quote
quote:
Originally posted by rmg1

Thanks for that.
Is it possible to add in the table name as well?

You can add TABLE_NAME column (and any other column returned by the INFORMATION_SCHEMA.COLUMNS catalog view).
exec sp_MSforeachdb 
'select 
	''?'' AS [Database],
	[COLUMN_NAME],
	[CHARACTER_MAXIMUM_LENGTH],
	[TABLE_NAME]
 from 
	[?].[Information_schema].[columns]
 where
	[DATA_TYPE] = ''nvarchar''';

Edited by - sunitabeck on 07/04/2012 10:23:05
Go to Top of Page

rmg1
Posting Yak Master

245 Posts

Posted - 07/04/2012 :  10:48:51  Show Profile  Reply with Quote
Thanks for that, it'll save ma about 2 hours searching.
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.05 seconds. Powered By: Snitz Forums 2000