I have joined a new project which is quite complex. We are using SQL Server 2012 and has many databases(15+). Each table in each database has many columns(more than 25 - 50).
There are thousands of records in each table. I want to view the data in the best possible manner. However, there are nulls for some columns. It has become quite difficult for me to get an idea on the data exists in each table.
Here is my question for sample data.
Id |Column1 |Column2 | Column3 |Column4 |............|ColumnNth
1 | sam |*NULL* | 2 | *NULL* |............|mike
2 | jack |2014-02-03| *NULL* | 6 |............|*NULL*
3 | Rav | *NULL* | *NULL* | *NULL*|............|Jim
4 | *NULL* |2013-09-08| 7 | 922 |............|Rocky
5 | Tom |2014-10-08| 11 | 212 |............|Mary
...
...
so on...
For example, below is the count of NULLs of all the columns for each rows. [It is not a table in the database. "Count of NULLs" is not a column of the initial table. I have shown this just for easy explanation]
ID| Count of NULLs
1 | 5
2 |2
3 |10
4 |1
5 |0
6 | 1
7 | 2
8 | 3
...
...
and so on..
Now I want to view the data with less number of NULLs in the top. The order of data should be in the below order. I have shown only the order of IDs so that not to type all the data(columns) again.
ID
5
4
6
2
7
8
1
...
...
3
...
so on...
Explanation of output:
1. The total number of nulls for all the columns for row(ID = 5) is ZERO. This should be first.
2. The total number of nulls for all the columns for row(ID = 4) and row(ID = 6) is ONE. Rows with ID = 4 and ID = 6 should be followed by ID = 5.
... so on...
In the output, I don't want NULL to be replaced by other value like 0 or 1 or blank value etc. So NULL should be displayed as it is in the output if it exists in the initial table.
I want to view all the columns
Please give me a stored procedure or query or set of queries which is generic. As the number of columns are more than 25-40, the SP or query should work without specifying the column names. If it is Stored procedure, then the Table name would be the input for it.
I assume that it is quite complex to write the SP for this requirement. But what I have asked is a real time situation where a new developer faces when he/she joins a support/maintanence project with complex and huge databases that was already built and contains huge data. Thanks