Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

How can we find the Null values on indexed columns from all tables in SQL Server. Expand / Collapse
Author
Message
Posted Friday, June 7, 2013 3:26 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:21 PM
Points: 42,495, Visits: 35,566
bslchennai (6/6/2013)
Yes, i have to take care of all null values because in our database we should not insert null values on indexed columns.


Why not? What's the problem with null values in an indexed column?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1461011
Posted Friday, June 7, 2013 7:33 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:14 PM
Points: 13,139, Visits: 11,980
Thanks for sharing Lowell. This is a great script.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1461074
Posted Friday, June 7, 2013 2:49 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 5:41 PM
Points: 185, Visits: 920
GilaMonster (6/7/2013)
bslchennai (6/6/2013)
Yes, i have to take care of all null values because in our database we should not insert null values on indexed columns.


Why not? What's the problem with null values in an indexed column?
Totally agree.
If tomorrow you decide to create new indexes then you won't be able to do it easily because of this ridiculous requirement.



Alex Suprun
Post #1461231
Posted Sunday, June 16, 2013 9:01 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 13, 2013 3:23 AM
Points: 2,415, Visits: 84
Finally I have created one script and it is working for me.

DROP TABLE #temp
DROP TABLE #temp1
DROP TABLE #temp2

--CURSOR TO FIND ALL INDEX'S IN ALL TABLES.
DECLARE @TabName varchar(100)
CREATE TABLE #temp ([Sl_No] [int] IDENTITY(1,1) NOT NULL,TabName varchar(200), IndexName varchar(200), IndexDescr varchar(200), IndexKeys varchar(200), IndexSize int)

DECLARE cur CURSOR FAST_FORWARD LOCAL FOR
SELECT name FROM sysobjects WHERE xtype = 'U' order by name
OPEN cur

FETCH NEXT FROM cur INTO @TabName
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #temp (IndexName, IndexDescr, IndexKeys)
EXEC sp_helpindex @TabName

UPDATE #temp SET TabName = @TabName WHERE TabName IS NULL

FETCH NEXT FROM cur INTO @TabName
END
CLOSE cur
DEALLOCATE cur

DECLARE @ValueCoef int
SELECT @ValueCoef = low FROM Master.dbo.spt_values WHERE number = 1 AND type = N'E'

UPDATE #temp SET IndexSize =((CAST(sysindexes.used AS bigint) * @ValueCoef)/1024)/1024
FROM sysobjects INNER JOIN sysindexes ON sysobjects.id = sysindexes.id
INNER JOIN #temp T ON T.TabName = sysobjects.name AND T.IndexName = sysindexes.name

GO

--CURSOR TO FIND ALL INDEXED COLUMNS WHIC IS HAVING NULL VALUES.
DECLARE @TabName1 varchar(200)
DECLARE @IndexName varchar(200)
DECLARE @IndexDescr varchar(200)
DECLARE @IndexKeys varchar(200)
DECLARE @IndexSize int
DECLARE @Row_Count NUMERIC(18,0)
DECLARE @sSQL NVARCHAR(MAX)
DECLARE @Sl_No NUMERIC(18,0)
DECLARE @identity NUMERIC(18,0)
DECLARE @IntRowCount NUMERIC(18,0)
DECLARE @sql nvarchar(4000)

SET @IntRowCount = 0

CREATE TABLE #temp1 ([Sl_No] [int] IDENTITY(1,1) NOT NULL,TabName varchar(200), IndexName varchar(200),
IndexDescr varchar(200), IndexKeys varchar(200),
IndexSize int,Row_Count NUMERIC(18,0))

CREATE TABLE #temp2 ([Sl_No] [int] IDENTITY(1,1) NOT NULL,TabName varchar(200), IndexName varchar(200),
IndexDescr varchar(200), IndexKeys varchar(200),
IndexSize int,Row_Count NUMERIC(18,0))

DECLARE cur CURSOR FAST_FORWARD LOCAL FOR
SELECT TabName,IndexKeys,IndexName,IndexDescr,IndexSize,Sl_No
FROM #temp
ORDER BY Sl_No
OPEN cur

FETCH
NEXT FROM cur
INTO @TabName1,
@IndexKeys,
@IndexName,
@IndexDescr,
@IndexSize,
@Sl_No

WHILE @@FETCH_STATUS = 0
BEGIN

IF (CHARINDEX(',',@IndexKeys) > 0)
BEGIN
INSERT INTO #temp2(TabName,IndexName,IndexDescr,IndexKeys,IndexSize)
VALUES (@TabName1,@IndexName,@IndexDescr,@IndexKeys,@IndexSize)
END
ELSE
BEGIN

SELECT @sql = N'select @Row_Count = count(*) FROM ' + @TabName1 +
N' WHERE '+@IndexKeys+' is null'

EXEC sp_executesql @sql, N'@Row_Count NUMERIC(18,0) OUTPUT,@IndexKeys varchar(200)', @Row_Count OUTPUT, @IndexKeys

SET @Row_Count = ISNULL(@Row_Count,0)
IF @Row_Count > 0
BEGIN
INSERT INTO #temp1(TabName,IndexName,IndexDescr,IndexKeys,IndexSize,Row_Count)
VALUES (@TabName1,@IndexName,@IndexDescr,@IndexKeys,@IndexSize,@Row_Count)
END
END

FETCH
NEXT FROM cur
INTO @TabName1,
@IndexKeys,
@IndexName,
@IndexDescr,
@IndexSize,
@Sl_No
END
CLOSE cur
DEALLOCATE cur

SELECT Sl_No,TabName,IndexKeys,IndexName,IndexDescr,Row_Count FROM #temp1 ORDER BY TabName,Row_Count


Regards,
BSL
Post #1463960
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse