Forum Discussion

Jayasurya_C01's avatar
Jayasurya_C01
New Member
2 years ago
Solved

PowerBI query

I would like to get all database under the SQL server and all the tables under the each database of the server in powerBI like how I can see all data in sql server(SSMS). I just expecting like this f...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Jayasurya_C01 ,

     

    Try below sql code when you connect to sql server database:

     

    SELECT 
        d.name AS DatabaseName,
        s.name AS SchemaName,
        t.name AS TableName
    FROM 
        sys.databases d
    JOIN 
        sys.tables t ON d.database_id = DB_ID(d.name)
    JOIN 
        sys.schemas s ON t.schema_id = s.schema_id
    ORDER BY 
        d.name, s.name, t.name;
    

     

     

     

    M code:

    let
        Source = Sql.Database("***", "AdventureWorksDW2014", [Query="SELECT #(lf)    d.name AS DatabaseName,#(lf)    s.name AS SchemaName,#(lf)    t.name AS TableName#(lf)FROM #(lf)    sys.databases d#(lf)JOIN #(lf)    sys.tables t ON d.database_id = DB_ID(d.name)#(lf)JOIN #(lf)    sys.schemas s ON t.schema_id = s.schema_id#(lf)ORDER BY #(lf)    d.name, s.name, t.name;#(lf)"])
    in
        Source

     

    Best Regards,
    Adamk Kong

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.