title: SQL Server statistics for all tables within database
body: <p>I want to use this query but every time I executed it the result was empty. Do you have any idea why?</p>
  
  <pre><code>DECLARE @name VARCHAR(50)
  
  DECLARE db_cursor CURSOR FOR 
      SELECT name FROM sys.tables
  
  OPEN db_cursor 
  
  FETCH NEXT FROM db_cursor INTO @name
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
      SELECT 
          s.name AS statistics_name,
          c.name AS column_name,
          sc.stats_column_id
      FROM 
          sys.stats AS s 
      INNER JOIN 
          sys.stats_columns AS sc ON s.object_id = sc.object_id 
                                  AND s.stats_id = sc.stats_id
      INNER JOIN 
          sys.columns AS c ON sc.object_id = c.object_id 
                           AND c.column_id = sc.column_id
      WHERE 
          s.object_id = OBJECT_ID('@name');
  
      FETCH NEXT FROM db_cursor INTO @name
  END 
  </code></pre>
  
  <p>Thank you very much,</p>
  
  <p>David.</p>