SQL Server: quick and dirty data validation query?










2















Getting frequent updates on a database that is going through major changes. Need a quick way to check for abnormalities in the data. Over 1,000 tables; about 200
actually being used.



Would like to have a list of the tables with the most records at the top. Found and modified this code that seems to work:



select 
object_name(object_id) AS TableName,
sum(rows) AS TableRowCount
from
sys.partitions
where
index_id in (0, 1)
group by
object_id
order by
TableRowCount desc


However, it would be useful to also have an indication if columns are populated properly. Would be great to have percentages of valid rows as well as percentages of NULLs, blanks and zeros.



Stumbled across this snippet:



SELECT
TABLE_NAME,
COLUMN_NAME,
dbo.ExecuteScalarToInt('SELECT ISNULL(SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) * 100.00/count(*), 0) Percentage FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) Percentage
FROM
INFORMATION_SCHEMA.COLUMNS


However, this throws an error:




Cannot find either column "dbo" or the user-defined function or aggregate "dbo.ExecuteScalarToInt", or the name is ambiguous.




Hmmm. I deleted the dbo.ExecuteScalarToInt part:



SELECT 
TABLE_NAME,
COLUMN_NAME,
('SELECT ISNULL(SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) * 100.00/count(*), 0) Percentage FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) AS Percentage
FROM
INFORMATION_SCHEMA.COLUMNS
ORDER BY
TABLE_NAME ASC, COLUMN_NAME ASC


This works, but for the percentage column, a SQL query shows up. Tried the resulting SQL query--it works.



Tried substituting "exec", "sp_executesql", and "exec sp_executesql".
Got errors.



Here is what I'm imagining what the output should look similar to:



TableName TableRows ColumnName PctPopulatedRows PctNULLs PctBlanks PctZeros
LineItems 4,649,764 InventryNum 100.0% 0.0% 0.0% 0.0%
LineItems 4,649,764 Description 99.9% 0.0% 0.1% 0.0%
LineItems 4,649,764 Price 99.7% 0.0% 0.1% 0.2%
.
.
.
Invoices 385,532 InvoiceNum 100.0% 0.0% 0.0% 0.0%
Invoices 385,532 DateTime 99.6% 0.2% 0.1% 0.1%
Invoices 385,532 TotalAmt 99.8% 0.0% 0.2% 0.0%
.
.
.
Customers 64,906 CustomerNum 100.0% 0.0% 0.0% 0.0%
Customers 64,906 CustStreet 99.8% 0.0% 0.2% 0.0%
Customers 64,906 CustZip 99.7% 0.1% 0.2% 0.0%


Tired of banging my head against this. Can anyone straighten me out to get closer to a solution?



Many thanks for taking a look.










share|improve this question
























  • ExecuteScalarToInt is a custom scalar user defined function. Wherever you lifted that from might have the definition for it. You'd need to create it in the same database. If a NULL or blank is legitimately an invalid piece of data then you should have constraints stopping that. Of course in the real world that can be difficult.

    – Nick.McDermaid
    Nov 13 '18 at 3:47











  • What you required can be done but not in one single query but utilize dynamic sql + temp table. Looks like it will be a complex query (the dynamic part)

    – Squirrel
    Nov 13 '18 at 4:12











  • You basically need to use a cursor to populate some dynamic SQL... like this stackoverflow.com/questions/25559088/…

    – Nick.McDermaid
    Nov 13 '18 at 11:08















2















Getting frequent updates on a database that is going through major changes. Need a quick way to check for abnormalities in the data. Over 1,000 tables; about 200
actually being used.



Would like to have a list of the tables with the most records at the top. Found and modified this code that seems to work:



select 
object_name(object_id) AS TableName,
sum(rows) AS TableRowCount
from
sys.partitions
where
index_id in (0, 1)
group by
object_id
order by
TableRowCount desc


However, it would be useful to also have an indication if columns are populated properly. Would be great to have percentages of valid rows as well as percentages of NULLs, blanks and zeros.



Stumbled across this snippet:



SELECT
TABLE_NAME,
COLUMN_NAME,
dbo.ExecuteScalarToInt('SELECT ISNULL(SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) * 100.00/count(*), 0) Percentage FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) Percentage
FROM
INFORMATION_SCHEMA.COLUMNS


However, this throws an error:




Cannot find either column "dbo" or the user-defined function or aggregate "dbo.ExecuteScalarToInt", or the name is ambiguous.




Hmmm. I deleted the dbo.ExecuteScalarToInt part:



SELECT 
TABLE_NAME,
COLUMN_NAME,
('SELECT ISNULL(SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) * 100.00/count(*), 0) Percentage FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) AS Percentage
FROM
INFORMATION_SCHEMA.COLUMNS
ORDER BY
TABLE_NAME ASC, COLUMN_NAME ASC


This works, but for the percentage column, a SQL query shows up. Tried the resulting SQL query--it works.



Tried substituting "exec", "sp_executesql", and "exec sp_executesql".
Got errors.



Here is what I'm imagining what the output should look similar to:



TableName TableRows ColumnName PctPopulatedRows PctNULLs PctBlanks PctZeros
LineItems 4,649,764 InventryNum 100.0% 0.0% 0.0% 0.0%
LineItems 4,649,764 Description 99.9% 0.0% 0.1% 0.0%
LineItems 4,649,764 Price 99.7% 0.0% 0.1% 0.2%
.
.
.
Invoices 385,532 InvoiceNum 100.0% 0.0% 0.0% 0.0%
Invoices 385,532 DateTime 99.6% 0.2% 0.1% 0.1%
Invoices 385,532 TotalAmt 99.8% 0.0% 0.2% 0.0%
.
.
.
Customers 64,906 CustomerNum 100.0% 0.0% 0.0% 0.0%
Customers 64,906 CustStreet 99.8% 0.0% 0.2% 0.0%
Customers 64,906 CustZip 99.7% 0.1% 0.2% 0.0%


Tired of banging my head against this. Can anyone straighten me out to get closer to a solution?



Many thanks for taking a look.










share|improve this question
























  • ExecuteScalarToInt is a custom scalar user defined function. Wherever you lifted that from might have the definition for it. You'd need to create it in the same database. If a NULL or blank is legitimately an invalid piece of data then you should have constraints stopping that. Of course in the real world that can be difficult.

    – Nick.McDermaid
    Nov 13 '18 at 3:47











  • What you required can be done but not in one single query but utilize dynamic sql + temp table. Looks like it will be a complex query (the dynamic part)

    – Squirrel
    Nov 13 '18 at 4:12











  • You basically need to use a cursor to populate some dynamic SQL... like this stackoverflow.com/questions/25559088/…

    – Nick.McDermaid
    Nov 13 '18 at 11:08













2












2








2








Getting frequent updates on a database that is going through major changes. Need a quick way to check for abnormalities in the data. Over 1,000 tables; about 200
actually being used.



Would like to have a list of the tables with the most records at the top. Found and modified this code that seems to work:



select 
object_name(object_id) AS TableName,
sum(rows) AS TableRowCount
from
sys.partitions
where
index_id in (0, 1)
group by
object_id
order by
TableRowCount desc


However, it would be useful to also have an indication if columns are populated properly. Would be great to have percentages of valid rows as well as percentages of NULLs, blanks and zeros.



Stumbled across this snippet:



SELECT
TABLE_NAME,
COLUMN_NAME,
dbo.ExecuteScalarToInt('SELECT ISNULL(SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) * 100.00/count(*), 0) Percentage FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) Percentage
FROM
INFORMATION_SCHEMA.COLUMNS


However, this throws an error:




Cannot find either column "dbo" or the user-defined function or aggregate "dbo.ExecuteScalarToInt", or the name is ambiguous.




Hmmm. I deleted the dbo.ExecuteScalarToInt part:



SELECT 
TABLE_NAME,
COLUMN_NAME,
('SELECT ISNULL(SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) * 100.00/count(*), 0) Percentage FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) AS Percentage
FROM
INFORMATION_SCHEMA.COLUMNS
ORDER BY
TABLE_NAME ASC, COLUMN_NAME ASC


This works, but for the percentage column, a SQL query shows up. Tried the resulting SQL query--it works.



Tried substituting "exec", "sp_executesql", and "exec sp_executesql".
Got errors.



Here is what I'm imagining what the output should look similar to:



TableName TableRows ColumnName PctPopulatedRows PctNULLs PctBlanks PctZeros
LineItems 4,649,764 InventryNum 100.0% 0.0% 0.0% 0.0%
LineItems 4,649,764 Description 99.9% 0.0% 0.1% 0.0%
LineItems 4,649,764 Price 99.7% 0.0% 0.1% 0.2%
.
.
.
Invoices 385,532 InvoiceNum 100.0% 0.0% 0.0% 0.0%
Invoices 385,532 DateTime 99.6% 0.2% 0.1% 0.1%
Invoices 385,532 TotalAmt 99.8% 0.0% 0.2% 0.0%
.
.
.
Customers 64,906 CustomerNum 100.0% 0.0% 0.0% 0.0%
Customers 64,906 CustStreet 99.8% 0.0% 0.2% 0.0%
Customers 64,906 CustZip 99.7% 0.1% 0.2% 0.0%


Tired of banging my head against this. Can anyone straighten me out to get closer to a solution?



Many thanks for taking a look.










share|improve this question
















Getting frequent updates on a database that is going through major changes. Need a quick way to check for abnormalities in the data. Over 1,000 tables; about 200
actually being used.



Would like to have a list of the tables with the most records at the top. Found and modified this code that seems to work:



select 
object_name(object_id) AS TableName,
sum(rows) AS TableRowCount
from
sys.partitions
where
index_id in (0, 1)
group by
object_id
order by
TableRowCount desc


However, it would be useful to also have an indication if columns are populated properly. Would be great to have percentages of valid rows as well as percentages of NULLs, blanks and zeros.



Stumbled across this snippet:



SELECT
TABLE_NAME,
COLUMN_NAME,
dbo.ExecuteScalarToInt('SELECT ISNULL(SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) * 100.00/count(*), 0) Percentage FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) Percentage
FROM
INFORMATION_SCHEMA.COLUMNS


However, this throws an error:




Cannot find either column "dbo" or the user-defined function or aggregate "dbo.ExecuteScalarToInt", or the name is ambiguous.




Hmmm. I deleted the dbo.ExecuteScalarToInt part:



SELECT 
TABLE_NAME,
COLUMN_NAME,
('SELECT ISNULL(SUM(CASE WHEN ' + QUOTENAME(COLUMN_NAME) + ' IS NULL THEN 1 ELSE 0 END) * 100.00/count(*), 0) Percentage FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) AS Percentage
FROM
INFORMATION_SCHEMA.COLUMNS
ORDER BY
TABLE_NAME ASC, COLUMN_NAME ASC


This works, but for the percentage column, a SQL query shows up. Tried the resulting SQL query--it works.



Tried substituting "exec", "sp_executesql", and "exec sp_executesql".
Got errors.



Here is what I'm imagining what the output should look similar to:



TableName TableRows ColumnName PctPopulatedRows PctNULLs PctBlanks PctZeros
LineItems 4,649,764 InventryNum 100.0% 0.0% 0.0% 0.0%
LineItems 4,649,764 Description 99.9% 0.0% 0.1% 0.0%
LineItems 4,649,764 Price 99.7% 0.0% 0.1% 0.2%
.
.
.
Invoices 385,532 InvoiceNum 100.0% 0.0% 0.0% 0.0%
Invoices 385,532 DateTime 99.6% 0.2% 0.1% 0.1%
Invoices 385,532 TotalAmt 99.8% 0.0% 0.2% 0.0%
.
.
.
Customers 64,906 CustomerNum 100.0% 0.0% 0.0% 0.0%
Customers 64,906 CustStreet 99.8% 0.0% 0.2% 0.0%
Customers 64,906 CustZip 99.7% 0.1% 0.2% 0.0%


Tired of banging my head against this. Can anyone straighten me out to get closer to a solution?



Many thanks for taking a look.







sql sql-server sql-server-2014






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:40









marc_s

575k12811101257




575k12811101257










asked Nov 13 '18 at 3:22









SquareDonutSquareDonut

182




182












  • ExecuteScalarToInt is a custom scalar user defined function. Wherever you lifted that from might have the definition for it. You'd need to create it in the same database. If a NULL or blank is legitimately an invalid piece of data then you should have constraints stopping that. Of course in the real world that can be difficult.

    – Nick.McDermaid
    Nov 13 '18 at 3:47











  • What you required can be done but not in one single query but utilize dynamic sql + temp table. Looks like it will be a complex query (the dynamic part)

    – Squirrel
    Nov 13 '18 at 4:12











  • You basically need to use a cursor to populate some dynamic SQL... like this stackoverflow.com/questions/25559088/…

    – Nick.McDermaid
    Nov 13 '18 at 11:08

















  • ExecuteScalarToInt is a custom scalar user defined function. Wherever you lifted that from might have the definition for it. You'd need to create it in the same database. If a NULL or blank is legitimately an invalid piece of data then you should have constraints stopping that. Of course in the real world that can be difficult.

    – Nick.McDermaid
    Nov 13 '18 at 3:47











  • What you required can be done but not in one single query but utilize dynamic sql + temp table. Looks like it will be a complex query (the dynamic part)

    – Squirrel
    Nov 13 '18 at 4:12











  • You basically need to use a cursor to populate some dynamic SQL... like this stackoverflow.com/questions/25559088/…

    – Nick.McDermaid
    Nov 13 '18 at 11:08
















ExecuteScalarToInt is a custom scalar user defined function. Wherever you lifted that from might have the definition for it. You'd need to create it in the same database. If a NULL or blank is legitimately an invalid piece of data then you should have constraints stopping that. Of course in the real world that can be difficult.

– Nick.McDermaid
Nov 13 '18 at 3:47





ExecuteScalarToInt is a custom scalar user defined function. Wherever you lifted that from might have the definition for it. You'd need to create it in the same database. If a NULL or blank is legitimately an invalid piece of data then you should have constraints stopping that. Of course in the real world that can be difficult.

– Nick.McDermaid
Nov 13 '18 at 3:47













What you required can be done but not in one single query but utilize dynamic sql + temp table. Looks like it will be a complex query (the dynamic part)

– Squirrel
Nov 13 '18 at 4:12





What you required can be done but not in one single query but utilize dynamic sql + temp table. Looks like it will be a complex query (the dynamic part)

– Squirrel
Nov 13 '18 at 4:12













You basically need to use a cursor to populate some dynamic SQL... like this stackoverflow.com/questions/25559088/…

– Nick.McDermaid
Nov 13 '18 at 11:08





You basically need to use a cursor to populate some dynamic SQL... like this stackoverflow.com/questions/25559088/…

– Nick.McDermaid
Nov 13 '18 at 11:08












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273322%2fsql-server-quick-and-dirty-data-validation-query%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273322%2fsql-server-quick-and-dirty-data-validation-query%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo