Как считать пустые таблицы в базе данных?

Есть ли любой способ считать таблицы без строк в моей базе данных с использованием T-SQL оператор?

12
задан Sjon 9 October 2015 в 17:09
поделиться

3 ответа

равно () и хэш-коду (), у них много разных мест. равно (), если мы не переопределяем его из объекта, это означает, что если две переменные указывают на одну и ту же кучу объектов?

public  Class Student(){
  private int id;
  private  name;
  public Student(int id,String name){
  this.name=name;
  this.id=id; 
}

public void main(String[] args){
  Student A=new Student(20,'Lily');
  Student B=new Student(20,'Lily');
  boolean flag=A.equals(B)//flag=flase;
/*
 *Although they attribute the same, but they are two different objects, they point to     different memory
 */


@Override
public boolean equals(Object obj) {


  if (obj == null) {
    return false;
  }
  if (this == obj) {
    return true;
  }

  if (this.getClass() != obj.getClass()) {
    return false;
  }
  Student s=(Student)obj;
  return new Integer(this.id).equals(new Integer(s.id))&&this.name.equals(s.name);
  }

/**
  *Sometimes even though we Override  the equals, but we still can not determine whether   the *two objects the same,
  *In the collection object, such as HashSet, this time we have to Override the hashoCode ()
  */

public int hashCode(){
  return id + name.hashCode() ;
}
-121--1533667-

Я использую следующее:

SELECT t.NAME AS TableName, sum(p.rows) as RowCounts
    FROM sys.tables t
INNER JOIN sys.indexes i
    ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p
    ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
WHERE
    i.name IS NULL AND i.index_id <= 1
GROUP BY 
    t.NAME, i.object_id, i.index_id, i.name 
HAVING SUM(p.rows) = 0
-121--2814183-

с использованием производной таблицы.

SELECT * FROM
(
 SELECT 
  [TableName] = so.name, 
  [RowCount] = MAX(si.rows) 
 FROM 
  sysobjects so, 
  sysindexes si 
 WHERE 
  so.xtype = 'U' 
  AND 
  si.id = OBJECT_ID(so.name) 
 GROUP BY 
  so.name 
) sub
WHERE sub.[RowCount] = 0
15
ответ дан 2 December 2019 в 18:19
поделиться

Я использую следующее:

SELECT t.NAME AS TableName, sum(p.rows) as RowCounts
    FROM sys.tables t
INNER JOIN sys.indexes i
    ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p
    ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
WHERE
    i.name IS NULL AND i.index_id <= 1
GROUP BY 
    t.NAME, i.object_id, i.index_id, i.name 
HAVING SUM(p.rows) = 0
4
ответ дан 2 December 2019 в 18:19
поделиться

из khtan @ SQL Server Forums, это используется для drop всех пустых таблиц, может быть, вы могли бы адаптировать его для вывода счетчика?

declare @name varchar(128), @sql nvarchar(2000), @i int
select  @name = ''
while   @name < (select max(name) from sysobjects where xtype = 'U')
begin
    select @name = min(name) from sysobjects where xtype = 'U' and name > @name
    select @sql = 'select @i = count(*) from [' + @name + ']'
    exec sp_executesql @sql, N'@i int out', @i out
    if @i = 0
    begin
        select @sql = 'drop table [' + @name + ']'
        print @sql
            -- unmask next to drop the table
        -- exec (@sql)
    end
end

У меня нет SQLServer здесь, но я могу взять нож, если хотите.

1
ответ дан 2 December 2019 в 18:19
поделиться
Другие вопросы по тегам:

Похожие вопросы: