Count null columns in each row - SQL - sql-server

I want to count number of columns that are null or = '' in each row in SQL. And group by Row_ID.
Something like this:
SELECT
Row_ID, COUNT(*) AS 'cnt_blankCol'
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_catalog = 'db'
AND table_name = 'tblName'
AND COLUMNS IS NULL OR COLUMNS = ''
GROUP BY
Row_ID
ORDER BY
COUNT(*)
Thank you.

Information_Schema tables are metadata tables that contain information about the database objects them selves, it does not contain the actual data from the tables, and it does not contain data aggregates per object.
This can not be done querying information_schema. Perhaps the Op can update the question and give a scenario of the goal of the question.

Related

Snowflake Information Schema behavior for View tables

I am new to snowflake. I am creating a view in snowflake as below
create view TABLENAME_VIEW as select * from test.stage.TABLENAME;
while running a select statement on information_schema.tables, I am getting the row_count of view table as null whereas doing select count(*) on the view tables gives the count.
Can someone please help on this.
TABLE_NAME
ROW_COUNT
TABLE_CATALOG
TABLE_TYPE
TABLENAME
5
TEST
BASE TABLE
TABLENAME_VIEW
NULL
TEST
VIEW
select count(*) from TABLENAME_VIEW;
COUNT(*)
5
Views are not tables (unless it an materializes view). So they have no rows. When you select from the view it becomes a projection of the underlying table/s
This can be seen by creating another view that has a “WHERE false” thus will return 0 to you select count(*) from the will return 0, but it will have no row. OR if your view had “limit 1”. It will have a count of 1, but which one is the magic part.

How to see the data types of all columns in SQL Sever Management System

I could not find answer to this question, despite it being very basic. How do I know whats the data type of all columns in SQL Server management System?
Col1 Col2 Col3 and so on
I wish to know the datatypes of each column in say Table1 where Table1 is the name of my table .
There are couple of options to see the data types of columns of the desired table -
Option 1
sp_help <tableName> e.g. sp_help Table1
Option 2
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Table1'
Option 3
Expand the Tables
Expand the desired table
Expand the columns
There are many several way to do this, one of them is to use schema :
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'Table1' or
COLUMN_Name = 'col1';

SQL Server stored procedure error handling with if exists

I have five databases, In that three databases having column Countries and two databases having column Countryrelease..
I am using a cursor, so if I use exists that particular column is throwing an error that column not exists how to handle this one.
Syntax
if exists(select 1 from table where column name='Countries')
select do some operation
else
select do some operation
You want to make use of the meta data within the SQL instance.
This will work for you ...
if (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YourTableName' AND COLUMN_NAME = 'Countries') = 1
-- The "Countries" column exists
select do some operation
else
select do some operation

SQL Count Records by Columns (Data Points)

I am pretty familiar with the COUNT function in SQL, but I don't know how to use it to achieve the results I'm looking for.
I manage a data warehouse with routine loads and I would like to report the number of raw data points (defined by #records * #columns) each time I load records into a table.
I can use the COUNT function to get the #records in the formula, but how can I dynamically count columns in a table? From there I can do the math.
Thanks!
You can query the schema tables where table meta data is stored in sql server.
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTable'
SELECT COUNT(*) FROM MyTable

How to get exact two columns of table(s) from a entair database in SQL server

There are two tables - Entity table and Account table. Entity table having EntityID and Account table having AccountID. There is a 3rd table EntityAccountAssociation which contains both EntityID and AccountID. - This I know.
My scenario is; suppose in a product support, I do not know about this 3rd table (and no one is there to tell me) , then, is there any query to find out this 3rd table to get the relationship in a huge database with 100s of table?
For Example: In that query, I will pass these two column name as parameter and it will show me on which particular table these two columns exists.
Please help and let me know. Thanks.
If all you're looking for is a query that returns table(s) with both columns, this would do:
-- DECLARE #col1 NVARCHAR(255) = 'X', #col2 NVARCHAR(255) = 'Y'
SELECT TABLE_SCHEMA + '.' + TABLE_NAME TablesContainingBoth
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN (#col1, #col2)
GROUP BY TABLE_SCHEMA + '.' + TABLE_NAME
HAVING COUNT(*) > 1
If only one table had both columns, it would return one result.

Resources