http://stackoverflow.com/questions/63291/sql-select-columns-with-null-values-only
This addressed one of the scenarios I was attempting to eliminate, where the column was never used and only contained nulls. I was also looking for columns that contained the same value repeated in all columns. I therefore modified Charles Graham’s answer from Stack Overflow, and created the query below:
The modified query returns all columns that have the same value in every record. The query prints out results in the following format: Table – Column – Value.1: DECLARE @col varchar(255), @table varchar(255), @cmd varchar(max), @cmd2 varchar(max)2:3: DECLARE getTableName CURSOR FOR4: SELECT [name] FROM sys.tables5:6: OPEN getTableName7:8: FETCH NEXT FROM getTableName INTO @table9:10: WHILE @@FETCH_STATUS = 011: BEGIN12: DECLARE getinfo CURSOR FOR13: SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID14: WHERE t.Name = @table15:16: OPEN getinfo17:18: FETCH NEXT FROM getinfo INTO @col19:20: WHILE @@FETCH_STATUS = 021: BEGIN22: SELECT @cmd = 'IF NOT EXISTS (SELECT top 1 * FROM [' + @table + ']23: WHERE [' + @col + '] IS NOT NULL) BEGIN print ''' + @table + ' - ' +24: @col + ' - (null)'' END'
25: EXEC(@cmd)26:27: SELECT @cmd2 = 'SET ANSI_Warnings OFF DECLARE @count int, @val28: varchar(255) SELECT @count = (SELECT COUNT(DISTINCT [' + @col + '])29: FROM [' + @table + ']) IF (@count < 2) BEGIN SELECT @val = (SELECT TOP30: 1 [' + @col + '] FROM [' + @table + ']) print ''' + @table + ' - ' +31: @col + ' - '' + @val + '''' END'
32: EXEC (@cmd2)33:34: FETCH NEXT FROM getinfo INTO @col35: END36:37: CLOSE getinfo38: DEALLOCATE getinfo39:40: FETCH NEXT FROM getTableName INTO @table41:42: END43:44: CLOSE getTableName45: DEALLOCATE getTableName46:
The second query that I found useful while testing the success of my migration code was the following query written by Mitchel Sellers (http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/121/determing-sql-server-table-size.aspx).
This query returns the record count for all tables, as well as information on the amount of space used by each table.
These two queries were both extremely useful during the database design and migration process.1: CREATE PROCEDURE GetAllTableSizes2: AS
3: /*
4: Obtains spaced used data for ALL user tables in the database5: */6: DECLARE @TableName VARCHAR(100) --For storing values in the cursor7:8: --Cursor to get the name of all user tables from the sysobjects listing9: DECLARE tableCursor CURSOR
10: FOR
11: select [name]
12: from dbo.sysobjects13: where OBJECTPROPERTY(id, N'IsUserTable') = 1
14: FOR READ ONLY
15:16: --A procedure level temp table to store the results
17: CREATE TABLE #TempTable18: (19: tableName varchar(100),20: numberofRows varchar(100),21: reservedSize varchar(50),22: dataSize varchar(50),23: indexSize varchar(50),24: unusedSize varchar(50)25: )26:27: --Open the cursor28: OPEN tableCursor29:30: --Get the first table name from the cursor
31: FETCH NEXT FROM tableCursor INTO @TableName
32:33: --Loop until the cursor was not able to fetch34: WHILE (@@Fetch_Status >= 0)
35: BEGIN36: --Dump the results of the sp_spaceused query to the temp table37: INSERT #TempTable38: EXEC sp_spaceused @TableName39:40: --Get the next table name41: FETCH NEXT FROM tableCursor INTO @TableName
42: END
43:44: --Get rid of the cursor45: CLOSE tableCursor46: DEALLOCATE tableCursor47:48: --Select all records so we can use the reults
49: SELECT *
50: FROM #TempTable51:52: --Final cleanup!53: DROP TABLE #TempTable54:55: GO
No comments:
Post a Comment