简体   繁体   中英

SQL Server 2008 - How to display only certain tables (Restrictions on where clause)

I have the following script

DECLARE @sql varchar(2000);
DECLARE @tableName sysname; 
DECLARE @columnName sysname;

DECLARE theCursor CURSOR FOR
   SELECT TABLE_NAME, COLUMN_NAME
   FROM INFORMATION_SCHEMA.COLUMNS
   WHERE
      DATA_TYPE = 'date' or DATA_TYPE = 'datetime'

OPEN theCursor;
FETCH NEXT FROM theCursor INTO @tableName, @columnName;

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = 'SELECT ''' + @tableName + ''' as [TabelName], ' + 
               '''' + @columnName + ''' AS [ColumnName], ' +
               'DATEPART(yy, ' + QUOTENAME(@columnName) + 
               ') AS [Year], COUNT(1) AS [NumberofRows] FROM ' + QUOTENAME(@tableName) + 
               ' GROUP BY DATEPART(yy, ' + QUOTENAME(@columnName) + ')';
    PRINT @sql;
    EXEC (@sql);

    FETCH NEXT FROM theCursor INTO @tableName, @columnName;
END

CLOSE theCursor
DEALLOCATE theCursor;

However there two problems with this code.

  1. The result is a list of tables, however I don't want any of these tables to have _ symbol in them. I have tried WHERE TABLE_NAME not like '%_%' but that doesn't seem to get rid of all the tables with _ in it.

  2. The result displays a list of tables with some having no rows (because there is no result). How do I emit these tables from not showing in the results window, and only the tables that have rows show.

Thanks.

1) Your first problem occurs because _ is a wildcard character that means a single letter in sql server.

For example, if you wanted to search for only single character words you'd use like '_' Putting square braces around the underscore will escape it, so your query would be:

WHERE 
  (DATA_TYPE = 'date' or DATA_TYPE = 'datetime')
  and TABLE_NAME not like '%[_]%'

2) In order to stop the tables without rows from showing, I would use a Having .

'GROUP BY DATEPART(yy, ' + QUOTENAME(@columnName) + ')' +
'Having Count(*) > 0'

This will only show tables that have one or more rows.

EDIT:

After a bit of conversation, it was revealed that the cursor should be restricted to only results with rows, not the exec'd query.

I added a test to see if there were any rows in your tables.

DECLARE @sql varchar(2000);
DECLARE @tableName sysname; 
DECLARE @columnName sysname;
DECLARE @count int;
DECLARE theCursor CURSOR FOR
   SELECT TABLE_NAME, COLUMN_NAME
   FROM INFORMATION_SCHEMA.COLUMNS
   WHERE
      (DATA_TYPE = 'date' or DATA_TYPE = 'datetime')
      and table_name not like '%[_]%'

OPEN theCursor;
FETCH NEXT FROM theCursor INTO @tableName, @columnName;

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = 'SELECT count(*) as cnt into ##temp_count FROM ' + QUOTENAME(@tableName)
    EXEC (@sql);
    select @count = cnt from ##temp_count
    drop table ##temp_count
    if(@count > 0)
    begin
      SET @sql = 'SELECT ''' + @tableName + ''' as [TabelName], ' + 
                 '''' + @columnName + ''' AS [ColumnName], ' +
                 'DATEPART(yy, ' + QUOTENAME(@columnName) + 
                 ') AS [Year], COUNT(1) AS [NumberofRows] FROM ' + QUOTENAME(@tableName) + 
                 ' GROUP BY DATEPART(yy, ' + QUOTENAME(@columnName) + ')';
      PRINT @sql;
      EXEC (@sql);
    end
    FETCH NEXT FROM theCursor INTO @tableName, @columnName;
END

CLOSE theCursor
DEALLOCATE theCursor;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM