Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
bholmstrom
Yak Posting Veteran
76 Posts |
Posted - 2013-03-08 : 10:11:15
|
Good morning, I have a query that produces a list of NULL value fields in a table. What I would like to do is have the user (me) select a table from a list of the tables in the database, or worst generate a list of all tables & fields that contain all NULL values (in other words the fields are not being used).Here is the initial queryDECLARE crs CURSOR LOCAL FAST_FORWARD FOR SELECT name FROM syscolumns WHERE id=OBJECT_ID('saleslogix.sysdba.c_acct_db_data')OPEN crsDECLARE @name sysnameFETCH NEXT FROM crs INTO @nameWHILE @@FETCH_STATUS = 0BEGIN EXEC('SELECT ''' + @name + ''' WHERE NOT EXISTS (SELECT * FROM saleslogix.sysdba.c_acct_db_data WHERE ' + @name + ' IS NOT NULL)') FETCH NEXT FROM crs INTO @nameENDCLOSE crsDEALLOCATE crsAnybody done this already, thanks in advance.Bryan Holmstrom |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-03-08 : 12:02:12
|
when you say "generate a list of all tables & fields (sic) that contain all NULL values (in other words the fields (sic) are not being used)" Do you mean where a column has a null value for every row, not just that that column is nullable? |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2013-03-08 : 12:06:00
|
This will give you all the tables and the fields where a null exists, you can modify it to give you only the fields where only nulls existsSET NOCOUNT ON CREATE TABLE #Nulls(TableName varchar(100),ColumnName varchar(100)) select 'IF EXISTS (SELECT * FROM Summary.' + TABLE_NAME+ ' WHERE [' + COLUMN_NAME + '] IS NULL) INSERT INTO #Nulls SELECT ' + '''' + REPLACE('!','!',TABLE_SCHEMA)+ ''' ,' + '''' + REPLACE('?','?',TABLE_NAME) + ''' ,' + '''' + REPLACE('!','!',COLUMN_NAME)+ ''''+ char(10) from information_schema.columns dbs where is_nullable = 'YES' JimEveryday I learn something that somebody else already knew |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-03-08 : 12:08:30
|
Argh, missed glossed over the IS NOT NULL part of the EXISTS clause..I don't know of a good way to get that information. But, to help speed things up you could use the INFORMATION_SCHEMA views to help eliminate checking columns that can't be null:SELECT COLUMN_NAMEFROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_SCHEMA = 'SchemaName' AND TABLE_NAME = 'TableName' AND IS_NULLABLE = 'YES' |
|
|
bholmstrom
Yak Posting Veteran
76 Posts |
Posted - 2013-03-08 : 12:43:30
|
JimF ...Thanks.....This should produce a table named Null in the tempdb directory, but I'm not seeing it?Also I cant run it twice:Server: Msg 2714, Level 16, State 6, Line 2There is already an object named '#Nulls' in the database.Bryan Holmstrom |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2013-03-08 : 12:47:07
|
You only need to create the table once, then select * from #nulls (in the same session you created the table)JimEveryday I learn something that somebody else already knew |
|
|
bholmstrom
Yak Posting Veteran
76 Posts |
Posted - 2013-03-08 : 14:05:07
|
Thanks, the table is being created. Now the problem is the "table" name is invalid. When is try and opoen it I get this error:dbo.#null__________________________________________________00000009D328Invalid object nameAny ideas, seems the dbo object name is too long or something.Bryan Holmstrom |
|
|
|
|
|
|
|