Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Believer
Advocate III
Advocate III

How do I combine custom SQL across databases into a single table?

I have the following code which efficiently concatenates the same table from several servers/databases.  (Thanks again, @watkinnc !!)  I added a declaration for some custom SQL.  How do I adapt the "Table.SelectRows..."  to use the custom SQL in place of the table?

 

let
TableName = "tblMyData",
CustomSQL = "Select top 10 Col1, Col2, Col4 From tblMyData Where Col5>0",

Source = Table.Combine({
Table.SelectRows(Sql.Database("SQLServer1", "MyDatabase1"), each [Item] = TableName){[Schema="dbo",Item=TableName]}[Data],
Table.SelectRows(Sql.Database("SQLServer2", "MyDatabase2"), each [Item] = TableName){[Schema="dbo",Item=TableName]}[Data],
Table.SelectRows(Sql.Database("SQLServer3", "MyDatabase3"), each [Item] = TableName){[Schema="dbo",Item=TableName]}[Data],
Table.SelectRows(Sql.Database("SQLServer4", "MyDatabase4"), each [Item] = TableName){[Schema="dbo",Item=TableName]}[Data],
Table.SelectRows(Sql.Database("SQLServer5", "MyDatabase5"), each [Item] = TableName){[Schema="dbo",Item=TableName]}[Data]})
in
Source

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @Believer 

 

Based on my test, you can try the following code. Sql.Database - PowerQuery M

 

let
    CustomSQL = "SELECT * FROM Table_1 WHERE Provider='a';",
    Table1 = Sql.Database("SQLServer1", "Database1", [Query=CustomSQL]),
    Table2 = Sql.Database("SQLServer2", "Database2", [Query=CustomSQL]),
    Source = Table.Combine({Table1, Table2})
in
    Source

 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

2 REPLIES 2
Believer
Advocate III
Advocate III

That works very well, thanks @v-jingzhang!

v-jingzhang
Community Support
Community Support

Hi @Believer 

 

Based on my test, you can try the following code. Sql.Database - PowerQuery M

 

let
    CustomSQL = "SELECT * FROM Table_1 WHERE Provider='a';",
    Table1 = Sql.Database("SQLServer1", "Database1", [Query=CustomSQL]),
    Table2 = Sql.Database("SQLServer2", "Database2", [Query=CustomSQL]),
    Source = Table.Combine({Table1, Table2})
in
    Source

 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors