"String or binary data would be truncated" message on SQL server 2008 with SP4

Dear all,

right now tried to run the following script on SQL server 2008 with SP4 to find the unused index.

	
						-- Create the temp table to store the unused indexes
						IF OBJECT_ID('TEMPDB..#UnusedIndexes') IS NOT NULL
						DROP TABLE #UnusedIndexes

						CREATE TABLE #UnusedIndexes
						(
						ID int identity(1,1)
						,DatabaseName VARCHAR(123)
						,SchemaName VARCHAR(123)
						,TableName VARCHAR(123)
						,IndexName VARCHAR(123)
						,User_Updates INT
						,User_SeeksScansLookups INT
						,System_Updates INT
						,System_SeeksScansLookups INT  
						)

						-- Load the unused indexes into the temp table
						EXEC sp_MSForEachDB 'USE [?];
						INSERT INTO #UnusedIndexes
						SELECT 
							DB_NAME() AS DatabaseName
							, SCHEMA_NAME(o.Schema_ID) AS SchemaName
							, OBJECT_NAME(ix_usage_stats.object_id) AS TableName
							, ix.name AS IndexName
							, ix_usage_stats.user_updates
							, ix_usage_stats.user_seeks + ix_usage_stats.user_scans + ix_usage_stats.user_lookups
													AS [User SeeksScansLookups]
							, ix_usage_stats.system_updates							
							, ix_usage_stats.system_seeks + ix_usage_stats.system_scans + ix_usage_stats.system_lookups
													AS [System SeeksScansLookups]
						FROM sys.dm_db_index_usage_stats ix_usage_stats
						INNER JOIN sys.indexes ix ON ix_usage_stats.object_id = ix.object_id
							AND ix_usage_stats.index_id = ix.index_id
						INNER JOIN sys.objects o ON ix.object_id = o.object_id
						WHERE ix_usage_stats.database_id = DB_ID()
							AND ix.name IS NOT NULL -- exclude heaps
							AND ix.type <> 1 -- exclude the clustered indexes
							AND o.is_ms_shipped = 0 -- exclude system objects
							AND o.type NOT IN(''F'', ''UQ'') -- exclude the foreign keys and unique contraints
							AND ix_usage_stats.user_seeks = 0
							AND ix_usage_stats.user_scans = 0
							AND ix_usage_stats.user_lookups = 0
						'

						-- Query the top 100 order by the user updates and generate the TSQL statement to drop the unused indexes
						SELECT top 100 uix.DatabaseName
						,uix.SchemaName 
						,uix.IndexName
						,uix.TableName
						,uix.User_Updates
						,uix.User_SeeksScansLookups
						,uix.System_Updates
						,uix.System_SeeksScansLookups
						,'DROP INDEX [' +uix.IndexName+ '] ON [' +uix.DatabaseName+ '].[' +uix.SchemaName+ '].[' +uix.TableName+ ']' as [DROP INDEX Statement]
						FROM #UnusedIndexes uix
						ORDER BY uix.User_Updates desc

						drop table #UnusedIndexes;

Open in new window


and it returns:

Msg 8152, Level 16, State 2, Line 2
String or binary data would be truncated.
The statement has been terminated.

(44 row(s) affected)

it works on SQL server 2008 with SP3, any problem you can see ?

 the problem is this part:

		-- Load the unused indexes into the temp table
						EXEC sp_MSForEachDB 'USE [?];
						INSERT INTO #UnusedIndexes
						SELECT 
							DB_NAME() AS DatabaseName
							, SCHEMA_NAME(o.Schema_ID) AS SchemaName
							, OBJECT_NAME(ix_usage_stats.object_id) AS TableName
							, ix.name AS IndexName
							, ix_usage_stats.user_updates
							, ix_usage_stats.user_seeks + ix_usage_stats.user_scans + ix_usage_stats.user_lookups
													AS [User SeeksScansLookups]
							, ix_usage_stats.system_updates							
							, ix_usage_stats.system_seeks + ix_usage_stats.system_scans + ix_usage_stats.system_lookups
													AS [System SeeksScansLookups]
						FROM sys.dm_db_index_usage_stats ix_usage_stats
						INNER JOIN sys.indexes ix ON ix_usage_stats.object_id = ix.object_id
							AND ix_usage_stats.index_id = ix.index_id
						INNER JOIN sys.objects o ON ix.object_id = o.object_id
						WHERE ix_usage_stats.database_id = DB_ID()
							AND ix.name IS NOT NULL -- exclude heaps
							AND ix.type <> 1 -- exclude the clustered indexes
							AND o.is_ms_shipped = 0 -- exclude system objects
							AND o.type NOT IN(''F'', ''UQ'') -- exclude the foreign keys and unique contraints
							AND ix_usage_stats.user_seeks = 0
							AND ix_usage_stats.user_scans = 0
							AND ix_usage_stats.user_lookups = 0
						'

Open in new window


can't see why, any idea?
LVL 1
marrowyungSenior Technical architecture (Data)Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Why are you creating the fields with varchar(123) when the system name is a nvarchar(128) data type?
0
marrowyungSenior Technical architecture (Data)Author Commented:
but that one running good on SP3 machine !

why ?

so I can try changing varchar(123)  to nvarchar(128)  ?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
but that one running good on SP3 machine !
 why ?
That's a good question but can only be answered by checking which bugs were corrected by SP4.

so I can try changing varchar(123)  to nvarchar(128)  ?
Yes, you should do that to match the data types.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marrowyungSenior Technical architecture (Data)Author Commented:
yeah, you are right!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.