Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server script to find LOB columns

Looking for a script to scan all tables in all SQL Server databases and list the columns which are large objects (TEXT, NTEXT, ,IMAGE VARCHAR(MAX), NVARCHAR(MAX), FILESTREAM, XML, VARBINARY).

while I probably can code this myself, I want a ready made script.

like image 945
Tony_Henrich Avatar asked Sep 27 '10 19:09

Tony_Henrich


People also ask

How do I find LOB columns in SQL Server?

Bookmark this question. Show activity on this post. Looking for a script to scan all tables in all SQL Server databases and list the columns which are large objects (TEXT, NTEXT, ,IMAGE VARCHAR(MAX), NVARCHAR(MAX), FILESTREAM, XML, VARBINARY).

What are LOB columns in SQL Server?

Large Objects (LOBs) are a set of datatypes that are designed to hold large amounts of data. A LOB can hold up to a maximum size ranging from 8 terabytes to 128 terabytes depending on how your database is configured. Storing data in LOBs enables you to access and manipulate the data efficiently in your application.

How do I get a list of columns in SQL?

In a query editor, if you highlight the text of table name (ex dbo. MyTable) and hit ALT + F1 , you'll get a list of column names, type, length, etc.


2 Answers

 select * from information_schema.columns where data_type in 
    ('TEXT', 'NTEXT','IMAGE' ,'XML', 'VARBINARY')
    or 
    (data_type = 'VARCHAR' and character_maximum_length = -1)
    OR
    (data_type = 'NVARCHAR' and character_maximum_length = -1)

Update Removed FILESTREAM from IN since the data_type is VARBINARY which is already captured

like image 183
Conrad Frix Avatar answered Sep 26 '22 16:09

Conrad Frix


Try this

select * from information_schema.columns
where DATA_TYPE in('text','ntext','xml','image')
or  (DATA_TYPE in('varchar','nvarchar','varbinary')
and CHARACTER_MAXIMUM_LENGTH = -1)
order by DATA_TYPE

filestream is stored as varbinary(max)

This will only capture varbinary(max), not varbinary(20) for example, if you also want that then move varbinary to the first condition, like this

 select * from information_schema.columns
    where DATA_TYPE in('text','ntext','xml','image','varbinary')
    or  (DATA_TYPE in('varchar','nvarchar')
    and CHARACTER_MAXIMUM_LENGTH = -1)
    order by DATA_TYPE
like image 23
SQLMenace Avatar answered Sep 26 '22 16:09

SQLMenace