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 statements.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: KrishanG
-- Create date: 05/08/2025
-- Procedure: spDDLCreateAllViews
-- Description:
-- This procedure dynamically creates views in SQL Server
-- for each table in a MySQL database via a linked server.
--
-- For every table in the specified MySQL schema:
-- - A corresponding SQL Server view (prefixed with 'lnk_') is created
-- in the target schema (@SQLTargetSchema).
-- - The views use OPENQUERY to directly select data from MySQL.
-- - If the table contains a 'deleted_at' column, a filter
-- (deleted_at IS NULL) is automatically applied in the view.
--
-- This allows SQL Server to query live data from MySQL as if it were
-- native tables, while filtering out soft-deleted records when applicable.
--
-- Parameters:
-- @LinkedServerName - The name of the linked server pointing to MySQL.
-- @MySQLDB - The name of the MySQL database/schema.
-- @SQLTargetSchema - The schema where the views will be created in SQL Server.
--
-- Notes:
-- - Views are created or altered using CREATE OR ALTER VIEW.
-- - Temporary metadata tables are used to detect available tables and columns.
-- - Make sure the linked server is properly configured before running.
-- =============================================
Create procedure spDDLCreateAllViews(
@LinkedServerName SYSNAME = 'SB',
@MySQLDB SYSNAME = 'schoolbox',
@SQLTargetSchema SYSNAME = 'dbo')
as
Begin
-- Test block
--DECLARE @LinkedServerName SYSNAME = 'SB';
--DECLARE @MySQLDB SYSNAME = 'schoolbox';
--DECLARE @SQLTargetSchema SYSNAME = 'dbo';
DECLARE @SQL NVARCHAR(MAX);
DECLARE @TableName SYSNAME;
SET NOCOUNT ON;
-- Step 1: Get all table names
SET @SQL = '
IF OBJECT_ID(''MySQLTables'') IS NOT NULL DROP TABLE MySQLTables;
SELECT TABLE_NAME
INTO MySQLTables
FROM OPENQUERY([' + @LinkedServerName + '],
''
SELECT TABLE_NAME
FROM information_schema.tables
WHERE table_schema = ''''' + @MySQLDB + '''''
AND table_type = ''''BASE TABLE''''
'')';
EXEC sp_executesql @SQL;
-- Step 2: Get tables that have a deleted_at column
SET @SQL = '
IF OBJECT_ID(''MySQLDeletedAtTables'') IS NOT NULL DROP TABLE MySQLDeletedAtTables;
SELECT DISTINCT TABLE_NAME
INTO MySQLDeletedAtTables
FROM OPENQUERY([' + @LinkedServerName + '],
''
SELECT TABLE_NAME
FROM information_schema.columns
WHERE table_schema = ''''' + @MySQLDB + '''''
AND COLUMN_NAME = ''''deleted_at''''
'')';
EXEC sp_executesql @SQL;
-- Step 3: Loop and create views
DECLARE cur CURSOR FOR
SELECT TABLE_NAME FROM MySQLTables;
OPEN cur;
FETCH NEXT FROM cur INTO @TableName;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @HasDeletedAt BIT = 0;
IF EXISTS (SELECT 1 FROM MySQLDeletedAtTables WHERE TABLE_NAME = @TableName)
SET @HasDeletedAt = 1;
-- Base query with WHERE 1=1
SET @SQL = '
CREATE OR ALTER VIEW ' + QUOTENAME(@SQLTargetSchema) + '.' + QUOTENAME('lnk_' + @TableName) + ' AS
SELECT * FROM OPENQUERY([' + @LinkedServerName + '],
''SELECT * FROM ' + @MySQLDB + '.' + @TableName + ' WHERE 1=1';
-- Conditionally add deleted_at filter
IF @HasDeletedAt = 1
SET @SQL += ' AND deleted_at IS NULL';
SET @SQL += ''');';
PRINT 'Creating view: ' + @TableName;
EXEC sp_executesql @SQL;
FETCH NEXT FROM cur INTO @TableName;
END
CLOSE cur;
DEALLOCATE cur;
end
go
Comments
Post a Comment