Skip to main content

Posts

Showing posts from August, 2025

How to Automatically Create SQL Server Views from MySQL Tables Using OPENQUERY (An alternative to ETL)

If you have a linked server from SQL Server to MySQL, you can automate importing data and creating views using dynamic SQL. This is useful when integrating external MySQL data into a Microsoft SQL Server reporting or analytics environment. 🔗 Setup: Linked Server to MySQL Make sure you have already set up your MySQL linked server in SQL Server (for example, named SB ), and that you can run queries like the following: SELECT * FROM OPENQUERY(SB, 'SELECT * FROM your_table'); ⚙️ Goal We want to dynamically create SQL Server views for all base tables in a MySQL database, using a format like: CREATE VIEW [dbo].[lnk_table_name] AS SELECT * FROM OPENQUERY(SB, 'SELECT * FROM table_name WHERE deleted_at IS NULL'); But not all MySQL tables have a deleted_at column. So, we will check whether the column exists before appending the WHERE clause. 🧠 Full SQL Script This SQL Server script loops through all MySQL tables and generates the appropriate view creation stat...