try this. I changed your dynamic sql to a call to sp_executesql to use an output variable and slightly changed your method of getting @tablenameDECLARE @TableName nvarchar(256), @SQL nvarchar(4000), @RCTR intSET @TableName = ''SET @RCTR = 0WHILE @TableName IS NOT NULLBEGIN SELECT @TableName = MIN(QUOTENAME(TABLE_NAME)) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND QUOTENAME(TABLE_NAME) > @TableName AND OBJECTPROPERTY( OBJECT_ID(QUOTENAME(TABLE_NAME) ), 'IsMSShipped') = 0 SET @SQL = 'select @rctr = count(*) from ' + @TableName EXEC sp_executesql @sql, N'@rctr int output', @rctr output IF @RCTR > 0 Print 'TableName: ' + @TableName + ' Row Count: ' + CAST(@RCTR AS varchar)END
Be One with the OptimizerTG