SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How can we find the Null values on indexed columns from all tables in SQL Server.


How can we find the Null values on indexed columns from all tables in SQL Server.

Author
Message
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88174 Visits: 45277
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, MVP, M.Sc (Comp Sci)
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


Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26307 Visits: 17553
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 Modens 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)
Alexander Suprun
Alexander Suprun
Mr or Mrs. 500
Mr or Mrs. 500 (515 reputation)Mr or Mrs. 500 (515 reputation)Mr or Mrs. 500 (515 reputation)Mr or Mrs. 500 (515 reputation)Mr or Mrs. 500 (515 reputation)Mr or Mrs. 500 (515 reputation)Mr or Mrs. 500 (515 reputation)Mr or Mrs. 500 (515 reputation)

Group: General Forum Members
Points: 515 Visits: 1516
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
bslchennai
bslchennai
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2439 Visits: 97
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search