0

I am trying to create a SQL code in SQL Server that dynamically selects all the tables in a specific database and then for each column in each table, counts the number of missing values and non-null values. I also want this result inserted into another table.

Is there any way I can do this without manually changing the column names for each:

Table Name - Column selection

I have a teradata code for the same which I tried to convert to SQL Server code. But I am unable to get the dynamic allocation and insertion parts right.

insert into temp
values (select  ''CAMP'',
    rtrim(''' || tablename || '''),
    rtrim(''' || columnname || '''),
    rtrim(''' || columnformat || '''),
    count(1),
    count(rtrim(upper(case when ' || columnname || '='''' then NULL else '|| columnname ||' end))),
    (cast (count(rtrim(upper(case when  ' || columnname || '='''' then NULL else  ' || columnname || ' end))) as float) / (cast (count(1) as float))) * 100,
    count(distinct rtrim(upper(case when ' || columnname || '='''' then NULL else '|| columnname ||' end))),
    min(rtrim(upper(case when ' || columnname || '='''' then NULL else '|| columnname ||' end))),
    max(rtrim(upper(case when ' || columnname || '='''' then NULL else '|| columnname ||' end))),
    min(len(rtrim(upper(case when ' || columnname || '='''' then NULL else '|| columnname ||' end)))),
    max(len(rtrim(upper(case when ' || columnname || '='''' then NULL else '|| columnname ||' end))))
from    ' || tablename ||')

Any help on this front would be great! Thanks!

4
  • Share what you have tried. Commented Jul 16, 2015 at 10:12
  • I edited the question to include what I tried. Any help would be great. Commented Jul 16, 2015 at 10:30
  • Instead of selecting data from different table and inserting them in another table, you can try view in SQL for this. Commented Jul 16, 2015 at 10:37
  • I am trying to use this to create a view of all the existing tables and some basic information about them. This will help me get an understanding of the kind of data I have in all my tables. Commented Jul 16, 2015 at 10:39

2 Answers 2

1

Not sure if you need a UNION or a JOIN, but in either case you can just use a three-part name for the object in the other database if you are using multi-database:

USE database1; // Your database name
GO
CREATE VIEW dbo.MyView
AS
    SELECT columns FROM dbo.Table1
    UNION ALL
    SELECT columns FROM database2.dbo.Table2; //second database
GO

select * from dbo.MyView // Getting all data from view

Hope that helps

Sign up to request clarification or add additional context in comments.

Comments

0

Does something like this help you:

    SELECT  [Table] = t.[name]
        , [Column] = c.[name]
    FROM    sys.tables t
    INNER   JOIN    sys.columns c
        ON  c.[object_id] = t.[object_id]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.